Scrollview嵌套百度地图MapView导致触摸事件冲突

来源:互联网 发布:catia软件书籍 编辑:程序博客网 时间:2024/06/05 14:28

问题

项目中需求经常会出现Scrollview嵌套百度地图MapView,如下:
但是这样嵌套会出现滑动百度地图的时候整个Scrollview跟着一起滑动了。

    <ScrollView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scrollbars="none">        <!--其他组件-->                <com.baidu.mapapi.map.MapView                    android:id="@+id/map_mapview"                    android:layout_width="match_parent"                    android:layout_height="@dimen/dimen_154dp"                    android:clickable="true"/>    </ScrollView>

解决

在触摸到MapView的时候拦截父类Scrollview的触摸事件即可

1,加一个自定义容器嵌套MapView,自定义容器如下:

public class MyMapView extends FrameLayout {    @Override    public boolean dispatchTouchEvent(MotionEvent ev) {        if (ev.getAction() == MotionEvent.ACTION_DOWN) {            getParent().requestDisallowInterceptTouchEvent(true);//拦截父类事件        } else if (ev.getAction() == MotionEvent.ACTION_UP) {                      getParent().requestDisallowInterceptTouchEvent(false);        }        return super.dispatchTouchEvent(ev);    }    public MyMapView(Context context) {        super(context);    }    public MyMapView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyMapView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }}

2,在布局中嵌套,如下:

<ScrollView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scrollbars="none">        <!--其他组件-->        <com.baibai.baibai.view.widget.MyMapView            android:layout_width="match_parent"            android:layout_height="@dimen/dimen_154dp">            <com.baidu.mapapi.map.MapView                android:id="@+id/map_mapview"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:clickable="true"/>        </com.baibai.baibai.view.widget.MyMapView>    </ScrollView>
阅读全文
2 0
原创粉丝点击