解决地图与scrollView滑动冲突

1.创建 自定义View MapContainer类

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

public class MapContainer extends RelativeLayout {
    private ScrollView scrollView;
    public MapContainer(Context context) {
        super(context);
    }

    public MapContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setScrollView(ScrollView scrollView) {
        this.scrollView = scrollView;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            scrollView.requestDisallowInterceptTouchEvent(false);
        } else {
            scrollView.requestDisallowInterceptTouchEvent(true);
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }
}

2更改布局

  1. <com.babyrun.mmsh.widget.MapContainer  
  2.      android:id="@+id/map_container"  
  3.       android:layout_width="match_parent"  
  4.       android:layout_height="wrap_content">  
  5.       <com.amap.api.maps2d.MapView  
  6.           android:id="@+id/map"  
  7.           android:layout_width="match_parent"  
  8.           android:layout_height="350dp" />  
  9.  </com.babyrun.mmsh.widget.MapContainer>  

3.

  1. scrollView = (ScrollView) findViewById(R.id.scrollview);  
  2.  map_container = (MapContainer) findViewById(R.id.map_container);  
  3.  map_container.setScrollView(scrollView);  

猜你喜欢

转载自blog.csdn.net/weixin_42376563/article/details/81143007