我有一个使用 Google 地图的小型迷你地图,其中显示了几个标记。应该只允许用户点击地图,以便我可以触发另一个操作。
但是,我认为 GoogleMap 正在消耗所有手势。地图嵌入在 ScrollView 中,而 ScrollView 嵌入在 Swipe 刷新容器中。
是否可以在 GoogleMap 上消费点击事件?
我试过了
mapFragment?.view?.setOnClickListener {
Log.i(TAG,"mapFragment clicked")
}
和
miniMapContainer?.setOnClickListener {
Log.i(TAG,"miniMapContainer Clicked")
}
以及在地图上关闭手势的一些不同组合。
class DetailsFragment : Fragment(R.layout.fragment_details) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Lookup the swipe container view
swipeContainer = view.findViewById(R.id.swipeContainer) as SwipeRefreshLayout
// Setup refresh listener which triggers new data loading
swipeContainer?.setOnRefreshListener {
// Your code to refresh the list here.
// Make sure you call swipeContainer.setRefreshing(false) once the network request has completed successfully.
refreshData()
}
// Configure the refreshing colors
swipeContainer?.setColorSchemeResources(
android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light
)
val mapFragment = childFragmentManager.findFragmentById(R.id.googleMap) as SupportMapFragment?
mapFragment?.getMapAsync(callback)
// Here is where I am trying to consume the click
mapFragment?.view?.setOnClickListener {
Log.i(TAG,"mapFragment clicked")
}
miniMapContainer?.setOnClickListener {
Log.i(TAG,"miniMapContainer Clicked")
}
}
@SuppressLint("MissingPermission")
private val callback = OnMapReadyCallback { googleMap ->
this.googleMap = googleMap.apply {
isTrafficEnabled = true
uiSettings?.isZoomGesturesEnabled = false
uiSettings?.isMapToolbarEnabled = true
uiSettings?.isRotateGesturesEnabled = false
uiSettings?.isScrollGesturesEnabled = false
uiSettings?.isZoomControlsEnabled = true
// uiSettings?.setAllGesturesEnabled(false)
}
this.googleMap.isMyLocationEnabled = true
mapIsReady = true
}
}
布局文件
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/frame_padding">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".ui.fragments.jobdetails.DetailsFragment">
<androidx.cardview.widget.CardView
android:id="@+id/miniMapContainer"
android:layout_width="0dp"
android:layout_height="@dimen/widget_map_size_height"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="@dimen/section_margin_vertical_20"
app:cardCornerRadius="@dimen/small_corner_radius"
app:cardElevation="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/section_notes">
<ProgressBar
android:id="@+id/progressBarMaps"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:indeterminate="true" />
<include
android:id="@+id/section_google_map"
layout="@layout/fragment_google_maps"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.cardview.widget.CardView>
</ScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
Google 地图布局文件