Android里scroll嵌套google map
来源:互联网 发布:儿童服装淘宝网 编辑:程序博客网 时间:2024/06/15 19:20
Android开发中在页面嵌套了google 地图,使用的时候发现地图不能拖动,跟scrollview事件有冲突。百度不给力,还是google好用点。
In this case, i work with the SupportMapFragment in Android Map V2 so the workaround is to make a custom SupportMapFragment class so we can override its touch event. This solution is based on gaucho’sanswer on related post in stackoverflow with some small changes made by me.
package net.londatiga.android.ui.fragment; import android.content.Context;import android.os.Bundle;import android.widget.FrameLayout; import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup; import com.google.android.gms.maps.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); } }}
In this class, we intercept the touch event by using TouchableWrapper class that extends the FrameLayout. There is also a custom listener OnTouchListener to dispatch the touch event to the main activity MyMapActivity that handles the map. When touch event occured, dispatchTouchEvent will be called and the listener mListener will handle it.
<?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>
package net.londatiga.android.ui; import net.londatiga.android.ui.fragment.WorkaroundMapFragment;import com.google.android.gms.maps.GoogleMap;import android.widget.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); } }); }}
Inside activity class, get the GoogleMap mMap and ScrollView container mScollView then set theOnTouchListener to the WorkaroundMapFragment. When touch event occured, onTouch handler will be called and we block the event being dispatched the ScrollView layout. This will make the map scrollable without the ScrollView being scrollable too.
- Android里scroll嵌套google map
- Android Scroll 中嵌套ListView
- 网页嵌套 google map api
- android GOOGLE MAP应用
- android google map
- android google map 气泡
- Android调用Google Map
- 使用Android Google Map
- Android Google map使用
- Android:Google Map学习
- android Google map 范例
- Android Google Map
- Android google map
- android google map开发
- android google map
- android google map v2
- Android调用Google Map
- android google map
- iOS基础控件-UITabBarController - 2 之UITabBarItem 详解,结合视图控制器和导航控制器的用法
- JavaScript 核心参考教程 内置对象
- Hadoop 常用命令
- 解决NDK开发中的 Method 'NewStringUTF' could not be resolved
- 嵌入式linux自动挂载U盘
- Android里scroll嵌套google map
- eXtremeDB-STAC测试1
- 计算机视觉领域
- Maven安装配置
- Java date format 时间格式化
- nyoj 744 蚂蚁的难题(一)
- jsp 导出excel时,长数字以科学计数法的形式展现问题?
- sysfs 文件系统(写得好)
- 深入探讨 Java 类加载器