Android handle scrolling of Google Map inside RecycleView

393 Views Asked by At

Haven't found a question/answer for this. When you put a Google Map inside a RecycleView (I'm using MapView inside a ViewHolder), the scrolling only works from left-to-right. Up-down scrolling is taken up by the RecycleView. Is there a way to provide normal (one finger) scrolling for the map view?

Thanks

2

There are 2 best solutions below

2
On

RecycleView get by default up and down surface scroll. You have to separate it from the main view. By using NestedScrollView you can get this. Put your googlemap in NestedScrollView.

0
On

This is how I've solved this -

I've created a custom view extending mapView and override dispatchTouchEvent()

class CustomMapView(context: Context, attrs: AttributeSet?): MapView(context, attrs) {

override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
    this.performClick()
    when (ev.action) {
        MotionEvent.ACTION_DOWN ->         // Disallow ScrollView to intercept touch events.
            this.parent.requestDisallowInterceptTouchEvent(true)
        MotionEvent.ACTION_UP ->         // Allow ScrollView to intercept touch events.
            this.parent.requestDisallowInterceptTouchEvent(false)
    }

    // Handle MapView's touch events.
    super.dispatchTouchEvent(ev)
    return true
    }
}

and used this to my xml -

    <com.demo.android.demo.ui.base.customviews.CustomMapView
        android:id="@+id/map_placeholder"
        android:layout_width="0dp"
        android:layout_height="700dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        map:mapType="none" />