我正在使用Google Maps API开发应用程序。我有一个布尔值,告诉我是否跟随用户移动。我想在用户拖动地图时将其设置为false。但是我该怎么做呢?那是我的代码
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/checkpoint"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"/>
在Java类中,我称之为
mapFragment = new MapFragment();
getFragmentManager().beginTransaction().add(R.id.map, mapFragment).commit();
getFragmentManager().executePendingTransactions();
您可以创建自定义的根布局,该布局可以监视触摸事件以映射片段并使用它代替默认值FrameLayout
:
public class CustomFrameLayout extends FrameLayout {
private GestureDetector gestureDetector;
private IDragCallback dragListener;
public CustomFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
gestureDetector = new GestureDetector(context, new GestureListener());
}
public interface IDragCallback {
void onDrag();
}
public void setOnDragListener(IDragCallback listener) {
this.dragListener = listener;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
gestureDetector.onTouchEvent(ev);
return false;
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
//that's when user starts dragging
if(dragListener != null) {
dragListener.onDrag();
}
return false;
}
}
}
========= your_activity_layout.xml:
<com.your.package.CustomFrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/checkpoint"
android:id="@+id/map"/>
========= YourActivity.java
CustomFrameLayout mapRoot = (CustomFrameLayout) findViewById(R.id.map);
mapRoot.setOnDragListener(this);
.......
@Override
public void onDrag() {
//reset your flag here
}
旁注:您知道您输入的android:name="com.google.android.gms.maps.SupportMapFragment"
属性FrameLayout
没有用..对吗?您通常android:name
在<fragment>
xml元素上指定属性。不是布局。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句