问题
I have a google map fragment on the top of my bottom sheet dialog. I disabled the draggable touch action on the bottom sheet behavior so that I could control the map. The problem is that I can't scroll the map using up or down touch actions because of my bottom sheet draggable disabled. I was thinking to disable the touch action of the bottom sheet behavior when the user touches the map but I don't know how to do this. How can I fix this?
回答1:
Try adding nestedScrollingEnabled="true"
to the bottom sheet layout:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="true"
app:behavior_hideable="true"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
回答2:
I have a BottomSheetDialogFragment
(parent View
) that contains SupportMapFragment
(child View
). Panning the map works only for horizontal gestures. As OP mentioned, this is because BottomSheet
and Map
touch events are in conflict when it comes to vertical gestures.
This is how I handled it. My BottomSheetDialogFragment
implements OnMapReadyCallback
and GoogleMap.OnCameraMoveStartedListener
.
override fun onMapReady(p0: GoogleMap) {
p0.setOnCameraMoveStartedListener(this)
}
override fun onCameraMoveStarted(reason: Int) {
when(reason) {
GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE -> {
// The user gestured on the map
childView?.parent?.requestDisallowInterceptTouchEvent(true)
}
GoogleMap.OnCameraMoveStartedListener.REASON_API_ANIMATION -> {
// The user tapped something on the map
childView?.parent?.requestDisallowInterceptTouchEvent(true)
}
GoogleMap.OnCameraMoveStartedListener.REASON_DEVELOPER_ANIMATION -> {
// The app moved the camera
childView?.parent?.requestDisallowInterceptTouchEvent(true)
}
}
}
When set as true
, requestDisallowInterceptTouchEvent()
does not allow the parent View
to intercept with the touch events of the child View
. I can now zoom in/out the map (horizontal and vertical gestures) in my bottom sheet dialog fragment.
来源:https://stackoverflow.com/questions/39330310/google-map-touch-on-bottom-sheet-dialog