scrollview 中 嵌套高德地图,上下滑动冲突 完美解决

1、自定义一个viewGroup,继承Relativelayout(或者别的layout) 
重点是重写了onInterceptTouchEvent(MotionEvent ev)和onTouchEvent(MotionEvent event)

[java]  view plain  copy
  1. /** 
  2.  *解决地图在主scrollview中滑动冲突的问题由于MapView被定义成final class,所以只能在容器中操作了 
  3.  Created by 苏七 on 2016/8/3 
  4.  */  
  5.   
  6. public class MapContainer extends RelativeLayout {  
  7.     private ScrollView scrollView;  
  8.     public MapContainer(Context context) {  
  9.         super(context);  
  10.     }  
  11.   
  12.     public MapContainer(Context context, AttributeSet attrs) {  
  13.         super(context, attrs);  
  14.     }  
  15.   
  16.     public void setScrollView(ScrollView scrollView) {  
  17.         this.scrollView = scrollView;  
  18.     }  
  19.   
  20.     @Override  
  21.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  22.         if (ev.getAction() == MotionEvent.ACTION_UP) {  
  23.             scrollView.requestDisallowInterceptTouchEvent(false);  
  24.         } else {  
  25.             scrollView.requestDisallowInterceptTouchEvent(true);  
  26.         }  
  27.         return false;  
  28.     }  
  29.   
  30.     @Override  
  31.     public boolean onTouchEvent(MotionEvent event) {  
  32.         return true;  
  33.     }  
  34. }  
2、在布局文件中,用上边定义的父控件包裹住高德地图的com.amap.api.maps2d.MapView.当然最外边是咱们的scrollview。

[java]  view plain  copy
  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、在代码中设置一下要关联的Scrollview

[java]  view plain  copy
  1. scrollView = (ScrollView) findViewById(R.id.scrollview);  
  2.  map_container = (MapContainer) findViewById(R.id.map_container);  
  3.  map_container.setScrollView(scrollView);  

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值