MapView inside a ScrollView? MapView inside a ScrollView? android android

MapView inside a ScrollView?


I have had a same problem for 10 days, but I got a solution a few minutes ago!!Here is the solution.I made a custom MapView and override onTouchEvent() like this.

@Overridepublic boolean onTouchEvent(MotionEvent ev) {    int action = ev.getAction();    switch (action) {    case MotionEvent.ACTION_DOWN:        // Disallow ScrollView to intercept touch events.        this.getParent().requestDisallowInterceptTouchEvent(true);        break;    case MotionEvent.ACTION_UP:        // Allow ScrollView to intercept touch events.        this.getParent().requestDisallowInterceptTouchEvent(false);        break;    }    // Handle MapView's touch events.    super.onTouchEvent(ev);    return true;}


A better/simpler way to do this without manipulating individual touch events.This will work if you are using MapView:

  @Override    public boolean dispatchTouchEvent(MotionEvent ev) {        /**         * Request all parents to relinquish the touch events         */        getParent().requestDisallowInterceptTouchEvent(true);        return super.dispatchTouchEvent(ev);    }

Full class:

public class CustomMapView extends MapView {    public CustomMapView(Context context) {        super(context);    }    public CustomMapView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public CustomMapView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    public CustomMapView(Context context, GoogleMapOptions options) {        super(context, options);    }    @Override    public boolean dispatchTouchEvent(MotionEvent ev) {        /**         * Request all parents to relinquish the touch events         */        getParent().requestDisallowInterceptTouchEvent(true);        return super.dispatchTouchEvent(ev);    }}

If you are using a MapFragment then you can put the fragment in a Custom View, and in the dispatchTouchEvent() make the requestDisallowInterceptTouchEvent call.


Make your own map and use it. It works fully for me.

public class CustomMapView extends MapView {public CustomMapView(Context context, AttributeSet attrs) {    super(context, attrs);}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {    switch (ev.getAction()) {    case MotionEvent.ACTION_UP:        System.out.println("unlocked");        this.getParent().requestDisallowInterceptTouchEvent(false);        break;    case MotionEvent.ACTION_DOWN:        System.out.println("locked");        this.getParent().requestDisallowInterceptTouchEvent(true);        break;    }    return super.dispatchTouchEvent(ev);}} 

In your layout xml,

<com.yourpackage.xxxx.utils.CustomMapView                android:id="@+id/customMap"                android:layout_width="match_parent"                android:layout_height="400dp"                android:layout_marginLeft="5dp"                android:layout_marginRight="5dp"                />