简体   繁体   中英

Android handle scrolling of Google Map inside RecycleView

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

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.

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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM