MapView inside ScrollView?

I would like to have a MapView inside ScrollView, however, when I try to scroll the map, ScrollView takes precedence! Is there a way to prioritize MapView when scrolling inside a map, and ScrollView otherwise?

Thank!

+43
android google-maps android-maps
Jul 01 2018-11-11T00:
source share
9 answers

I had the same problem for 10 days, but I got the solution a few minutes ago !! Here is the solution. I made my own MapView and redefined onTouchEvent () as follows.

@Override public 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 touch events. super.onTouchEvent(ev); return true; } 
+85
Jul 30 2018-11-11T00:
source share

Create your own map and use it. It works completely for me.

 public class CustomMapView extends MapView { public CustomMapView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public 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 xml layout,

 <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" /> 
+15
Apr 28 '16 at 8:51
source share

This is the best solution tutorial , works with Android v2 map ..

+11
Feb 10 '14 at 5:15
source share

For those who want all the working code. here

Custom map view class

 public class CustomMapView extends MapView { private ViewParent mViewParent; 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); } public void setViewParent(@Nullable final ViewParent viewParent) { //any ViewGroup mViewParent = viewParent; } @Override public boolean onInterceptTouchEvent(final MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (null == mViewParent) { getParent().requestDisallowInterceptTouchEvent(true); } else { mViewParent.requestDisallowInterceptTouchEvent(true); } break; case MotionEvent.ACTION_UP: if (null == mViewParent) { getParent().requestDisallowInterceptTouchEvent(false); } else { mViewParent.requestDisallowInterceptTouchEvent(false); } break; default: break; } return super.onInterceptTouchEvent(event); } } 

Xml action layout

  <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <location.to.your.CustomMapView android:id="@+id/mapView" android:layout_width="match_parent" android:layout_height="250dp" /> </ScrollView> 

Creating an instance of a custom map class in your activity or fragment

  CustomMapView mapView = (CustomMapView) findViewById(R.id.mapView); 

So that he enjoys

+11
Jun 06 '16 at 13:47
source share

You can create your own MapView as follows:

 public class CustomMapView extends MapView { private MapFragment.ControlLock mCallbackControl; public CustomMapView(Context context) { this(context, null); } public CustomMapView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomMapView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public CustomMapView(Context context, GoogleMapOptions options) { super(context, options); } public void setCallback(MapFragment.ControlLock callbackControl) { this.mCallbackControl = callbackControl; } @Override public boolean dispatchTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_UP: System.out.println("unlocked"); mCallbackControl.unlock(); /* Interface */ break; case MotionEvent.ACTION_DOWN: System.out.println("locked"); mCallbackControl.lock(); /* Interface */ break; } return super.dispatchTouchEvent(event); } } 
+9
Jul 09 '14 at 1:23
source share

This will work if you use 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 use MapFragment, you can place the fragment in a custom view, and make a call to requestDisallowInterceptTouchEvent in dispatchTouchEvent() .

+9
Oct 27 '15 at 23:19
source share

I tried with an override of MapView.onTouchEvent (...), but that did not work for me. Here is the code that works well (overriding MapView.onInterceptTouchEvent (...)):

 public class MyMapView extends MapView { private ViewParent mViewParent; //add constructors here public void setViewParent(@Nullable final ViewParent viewParent) { //any ViewGroup mViewParent = viewParent; } @Override public boolean onInterceptTouchEvent(final MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (null == mViewParent) { getParent().requestDisallowInterceptTouchEvent(true); } else { mViewParent.requestDisallowInterceptTouchEvent(true); } break; case MotionEvent.ACTION_UP: if (null == mViewParent) { getParent().requestDisallowInterceptTouchEvent(false); } else { mViewParent.requestDisallowInterceptTouchEvent(false); } break; default: break; } return super.onInterceptTouchEvent(event); } } 
+7
Jun 24 '15 at 15:00
source share

You can just put the MapView in the layout yourself and override onTouch or install the Click-Listener - the easiest way for me, since I need to touch all of MapView in my ScrollView.

+2
Jul 05 '12 at 15:00
source share

If you have a mapview in scroll mode, you must explicitly specify the follwing options in MapView:

 mMapView.setClickable(true); mMapView.setFocusable(true); mMapView.setDuplicateParentStateEnabled(false); 
+1
May 01 '13 at 11:11
source share



All Articles