Google map touch on bottom sheet dialog

2019-02-19 02:31发布

问题:

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 that contains SupportMapFragment. Panning the map works only for horizontal gestures, and as OP mentioned this is because BottomSheet and Map touch events are in conflict.

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
            view?.parent?.requestDisallowInterceptTouchEvent(true)
        }
        GoogleMap.OnCameraMoveStartedListener.REASON_API_ANIMATION -> {
            // The user tapped something on the map
            view?.parent?.requestDisallowInterceptTouchEvent(true)
        }
        GoogleMap.OnCameraMoveStartedListener.REASON_DEVELOPER_ANIMATION -> {
            // The app moved the camera
            view?.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 in my bottom sheet dialog fragment.