【问题标题】:Can I put a MapActivity inside a Scrollview?我可以将 MapActivity 放在 Scrollview 中吗?
【发布时间】:2020-02-26 11:43:56
【问题描述】:

我正在尝试在 ScrollView 页面中使用 MapActivity。当我运行应用程序对其进行测试时,无法正确单击地图,因为当用户尝试在地图上滑动时,滚动视图会接管并且有点难以在其上滑动。

这是它的外观(地图包含在最底部):

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CreateAds.FurnitureAd.CreateFunP1Fragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/lll"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <EditText
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/ad_title"
                android:inputType="textPersonName"
                android:textColor="@color/colorPrimary"
                tools:layout_editor_absoluteX="182dp"
                tools:layout_editor_absoluteY="16dp"
                android:importantForAutofill="no" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/rrr"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="@+id/lll"
            app:layout_constraintStart_toStartOf="@+id/lll"
            app:layout_constraintTop_toBottomOf="@+id/lll">

            <EditText
                android:id="@+id/editText2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:ems="10"
                android:hint="@string/offer_price"
                android:inputType="number"
                android:textColor="@color/colorPrimary"
                android:importantForAutofill="no" />
        </com.google.android.material.textfield.TextInputLayout>

        <TextView
            android:id="@+id/textView16"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="@string/currency"
            android:textColor="@color/colorPrimary"
            app:layout_constraintStart_toStartOf="@+id/spinner2"
            app:layout_constraintTop_toBottomOf="@+id/rrr" />

        <Spinner
            android:id="@+id/spinner2"
            style="@style/Widget.AppCompat.Spinner.Underlined"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="@+id/rrr"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="@+id/rrr"
            app:layout_constraintTop_toBottomOf="@+id/textView16" />

        <TextView
            android:id="@+id/textView18"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="@string/state"
            android:textColor="@color/colorPrimary"
            app:layout_constraintStart_toStartOf="@+id/spinner2"
            app:layout_constraintTop_toBottomOf="@+id/spinner2" />

        <Spinner
            android:id="@+id/spinner3"
            style="@style/Widget.AppCompat.Spinner.Underlined"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="@+id/spinner2"
            app:layout_constraintStart_toStartOf="@+id/spinner2"
            app:layout_constraintTop_toBottomOf="@+id/textView18" />

        <TextView
            android:id="@+id/textView20"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="@string/city"
            android:textColor="@color/colorPrimary"
            app:layout_constraintStart_toStartOf="@+id/spinner3"
            app:layout_constraintTop_toBottomOf="@+id/spinner3" />

        <Spinner
            android:id="@+id/spinner4"
            style="@style/Widget.AppCompat.Spinner.Underlined"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="@+id/spinner3"
            app:layout_constraintStart_toStartOf="@+id/spinner3"
            app:layout_constraintTop_toBottomOf="@+id/textView20" />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputLayout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="@+id/spinner4"
            app:layout_constraintStart_toStartOf="@+id/spinner4"
            app:layout_constraintTop_toBottomOf="@+id/spinner4">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="@string/location"
                android:inputType="text"
                android:importantForAutofill="no" />

        </com.google.android.material.textfield.TextInputLayout>

        <TextView
            android:id="@+id/textView24"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="@string/map"
            android:textColor="@color/colorPrimary"
            app:layout_constraintStart_toStartOf="@+id/textInputLayout"
            app:layout_constraintTop_toBottomOf="@+id/textInputLayout" />

        <include
            layout="@layout/activity_maps"
            android:layout_width="0dp"
            android:layout_height="450dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView24" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

这样实现有错吗?

【问题讨论】:

  • 可滚动视图上的任何内容都可能会丢失输入,因为滚动会消耗触摸输入。放置具有相反方向输入的东西(例如水平列表)或以其他方式将其限制为单击将地图扩展为自己的片段的事件。

标签: android android-mapview android-scrollview


【解决方案1】:

当您希望地图成为活动中的主要内容并且几乎是唯一内容时,将使用地图活动。请改用MapFragment。示例:

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.SupportMapFragment"/>

【讨论】:

  • 如果您的片段填满了整个活动,那么我的建议是将ScrollView 移动到活动并将MapFragment 用作单独的片段,或者如果您仍想将MapFragment 保留在里面你的CreateFunP1Fragment 然后看看这个问题:stackoverflow.com/questions/6672066/fragment-inside-fragment
  • 仍然遇到同样的问题,我无法访问它,滚动视图接管了触摸
  • 你怎么能在不到一分钟的时间里尝试过呢?试一试,并尝试应用@P Fuster 在他的评论中提到的内容
  • 冷静 :) 我的意思不是你留下的评论...我的意思是你在尝试 diffee=rent 时给出的片段解决方案,它开始工作的方式与包含工作的方式相同,这就是我删除的原因我的另外 2 个 cmets
【解决方案2】:

我们在这个场景中所做的首先是为地图添加一些边距/填充,以便用户可以在其一侧滚动。

但后来我们决定最好将 ScrollView 中的地图设置为非交互式地图,并在其顶部设置一个不可见的按钮,当用户点击该按钮时,会打开一个新的活动并最大化地图。并且在那个新活动中没有滚动,用户只能与地图交互或返回。

加载地图的片段是相同的,因此地图的代码是共享的,只是放置在两个不同的布局中(在未启用交互的滚动视图内和新活动中)。

【讨论】:

  • 是的,我只是想最好在地图顶部放置一个按钮,然后点击它会将他带到地图活动,感谢您的建议!
猜你喜欢
  • 2016-09-17
  • 2011-08-21
  • 2012-05-29
  • 1970-01-01
  • 2011-09-20
  • 2020-01-19
  • 2021-11-18
  • 1970-01-01
相关资源
最近更新 更多