MapView inside a ScrollView?

前端 未结 9 1274
时光取名叫无心
时光取名叫无心 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:46

    For those who want the whole working code . here it is

    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);
      }
    }
    

    Activity layout xml

      
    
        
    
    
    

    Instantiating the custom map class in your activity or fragment

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

    That's it enjoy

提交回复
热议问题