ScrollView嵌套百度地图MapView导致滑动冲突

项目中经常遇到ScrollView嵌套百度地图MapView,这时滑动百度地图时会发现水平左右方向可以滑动,上下滑动的时候就会发生冲突导致整个ScrollView一起滑动。

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!--其他组件-->

    <com.baidu.mapapi.map.MapView
        android:id="@+id/map_mapview"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:clickable="true" />
</LinearLayout>
</ScrollView>

解决方案

当滑动MapView时拦截父控件的触摸事件即可。

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、在MapView外嵌套上MyMapView就可以随心所欲的滑动百度地图了~

<com.applib.widget.MyMapView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.baidu.mapapi.map.MapView
                android:id="@+id/mapView"
                android:layout_width="match_parent"
                android:layout_height="220dp" />
</com.applib.widget.MyMapView>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值