解決google在scroll裡左右拖動的問題
2015-06-01 18:15
369 查看
在scroll裡使用google map後,發現地圖的拖拉事件有衝突問題,google一下,發現了以下解決方法
从技术上讲,将地图视图滚动型布局容器内可以使地图变得难以滚动。
这是因为滚动布局容器将拦截到触摸事件,以便在地图上就失去了触摸事件,使得难以滚动或平移。
无论是在Android地图V2或以前的版本都會发生这种情况。
在这种情况下,解决方法是创建一个自定义SupportMapFragment类,因此我们可以覆盖其触摸事件。
在这个类中,我们通过使用扩展的FrameLayout TouchableWrapper类拦截触摸事件。还有一个自定义侦听OnTouchListener调度触摸事件来处理该地图主activityMyMapActivity。当触摸事件发生,dispatchTouchEvent将被调用,听者mListener将会处理它。
xml
使用
从技术上讲,将地图视图滚动型布局容器内可以使地图变得难以滚动。
这是因为滚动布局容器将拦截到触摸事件,以便在地图上就失去了触摸事件,使得难以滚动或平移。
无论是在Android地图V2或以前的版本都會发生这种情况。
在这种情况下,解决方法是创建一个自定义SupportMapFragment类,因此我们可以覆盖其触摸事件。
public class WorkaroundMapFragment extends SupportMapFragment { private OnTouchListener mListener; @Override public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstance) { View layout = super.onCreateView(layoutInflater, viewGroup, savedInstance); TouchableWrapper frameLayout = new TouchableWrapper(getActivity()); frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent)); ((ViewGroup) layout).addView(frameLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); return layout; } public void setListener(OnTouchListener listener) { mListener = listener; } public interface OnTouchListener { public abstract void onTouch(); } public class TouchableWrapper extends FrameLayout { public TouchableWrapper(Context context) { super(context); } @Override public boolean dispatchTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mListener.onTouch(); break; case MotionEvent.ACTION_UP: mListener.onTouch(); break; } return super.dispatchTouchEvent(event); } } }
在这个类中,我们通过使用扩展的FrameLayout TouchableWrapper类拦截触摸事件。还有一个自定义侦听OnTouchListener调度触摸事件来处理该地图主activityMyMapActivity。当触摸事件发生,dispatchTouchEvent将被调用,听者mListener将会处理它。
xml
<?xml version="1.0" encoding="UTF-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/sv_container" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- other child views //--> <fragment android:tag="fragment_map" android:id="@+id/fragment_map" android:layout_width="match_parent" android:layout_height="175dp" android:layout_marginTop="@dimen/activity_horizontal_margin" class="net.londatiga.android.ui.fragment.WorkaroundMapFragment"/> </ScrollView>
使用
public class MyMapActivty extends MapActivity { private ScrollView mScrollView; private GoogleMap mMap; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_my_map); mMap = ((WorkaroundMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_map)).getMap(); mScrollView = (ScrollView) findViewById(R.id.sv_container); ((WorkaroundMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_map)).setListener(new WorkaroundMapFragment.OnTouchListener() { @Override public void onTouch() { mScrollView.requestDisallowInterceptTouchEvent(true); } }); } }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories