原地址:http://blog.csdn.net/waterthegreat/article/details/51945020
先说一下最终的解决方案。亲测可用。。。
1、自定义一个viewGroup,继承Relativelayout(或者别的layout)
重点是重写了onInterceptTouchEvent(MotionEvent ev)和onTouchEvent(MotionEvent event)
/**
*解决地图在主scrollview中滑动冲突的问题由于MapView被定义成final class,所以只能在容器中操作了
Created by 张玉水 on 2016/7/18.
*/
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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
2、在布局文件中,用上边定义的父控件包裹住高德地图的com.amap.api.maps2d.MapView.当然最外边是咱们的scrollview。
<com.babyrun.mmsh.widget.MapContainer
android:id="@+id/map_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.amap.api.maps2d.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="350dp" />
</com.babyrun.mmsh.widget.MapContainer>
3、在代码中设置一下要关联的Scrollview
scrollView = (ScrollView) findViewById(R.id.scrollview)
map_container = (MapContainer) findViewById(R.id.map_container)
map_container.setScrollView(scrollView)
上边三步做完后就可以完美解决问题。
另附:网上其他方法,都不怎么好用,亲测的。真心的。
一、这个代码网上出现很多次,但真的不管用啊。假的
mMapView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
scrollView.requestDisallowInterceptTouchEvent(false);
}else{
scrollView.requestDisallowInterceptTouchEvent(true);
}
return false;
}
});
如果是ListView嵌套在ScrollView中,解决方法是一样的。
二、重写scrollview,计算布局MapView所在的矩形区域,在矩形区域的时候,屏蔽scrollview的触摸事件的传递。下边的代码,有作用,但是效果不是很好啊。有的时候能动,有的时候不能。
/**
* Created by 张玉水 on 2016/7/18.
*/
public class LocationScrollView extends ScrollView {
public LocationScrollView(Context context) {
super(context);
}
public LocationScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LocationScrollView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
private Boolean mIfOnMap = false;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
int x = (int) ev.getX();
int y = (int) ev.getY();
Rect rect = new Rect();
if (ServiceLocationActivity.mapView != null) {
ServiceLocationActivity.mapView.measure(0,0);
ServiceLocationActivity.mapView.getDrawingRect(rect);
}
mIfOnMap = !rect.contains(x, y);
if (mIfOnMap) {
return false;
}else {
return super.onInterceptTouchEvent(ev);
}
}
}