MapView inside a ScrollView?

前端 未结 9 1279
时光取名叫无心
时光取名叫无心 2020-11-28 04:18

I would like to have a MapView inside a ScrollView, however when I try to scroll the map, the ScrollView takes priority! Is there a way to give the MapView priority when scr

9条回答
  •  天涯浪人
    2020-11-28 04:35

    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.

提交回复
热议问题