解决ScrollView嵌套百度地图滑动冲突
一、问题描述
scrollview中嵌套百度地图时会出现滑动冲突,地图无法滑动的情况。
二、期望结果
焦点在地图上时,只有地图移动,焦点在地图外部时,可以滑动scrollview。
三、解决方法
自定义包裹地图的容器
package com.aldx.kangdasupervisor.view; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.LinearLayout; /** * author: chenzheng * created on: 2019/7/9 11:20 * description: */ public class BaiduMapContainer extends LinearLayout { private MyScrollview scrollView; public BaiduMapContainer(Context context) { super(context); } public BaiduMapContainer(Context context, AttributeSet attrs) { super(context, attrs); } public void setScrollView(MyScrollview 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; } }
自定义scrollview
package com.aldx.kangdasupervisor.view; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.ViewConfiguration; import android.widget.ScrollView; /** * author: chenzheng * created on: 2017/7/5 8:46 * description: */ public class MyScrollview extends ScrollView { private int downX; private int downY; private int mTouchSlop; public MyScrollview(Context context) { super(context); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); } public MyScrollview(Context context, AttributeSet attrs) { super(context, attrs); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); } public MyScrollview(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); } @Override public boolean onInterceptTouchEvent(MotionEvent e) { int action = e.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: downX = (int) e.getRawX(); downY = (int) e.getRawY(); break; case MotionEvent.ACTION_MOVE: int moveY = (int) e.getRawY(); if (Math.abs(moveY - downY) > mTouchSlop) { return true; } } return super.onInterceptTouchEvent(e); } }
绑定scrollview与MapView
bannerContainer.setScrollView(myScrollview);
布局
<com.aldx.kangdasupervisor.view.BaiduMapContainer android:id="@+id/bannerContainer" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.baidu.mapapi.map.TextureMapView android:id="@+id/bmapView" android:layout_width="match_parent" android:layout_height="200dp" android:clickable="true" /> </com.aldx.kangdasupervisor.view.BaiduMapContainer>