检测Android Google地图上的拖动

Man*_*tro 4 android google-maps-android-api-2

我正在使用谷歌地图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"/>
Run Code Online (Sandbox Code Playgroud)

在java类中我称之为

    mapFragment = new MapFragment();
    getFragmentManager().beginTransaction().add(R.id.map, mapFragment).commit();
    getFragmentManager().executePendingTransactions();
Run Code Online (Sandbox Code Playgroud)

Pav*_*dka 9

您可以创建自定义根布局,该布局会监视触摸事件以映射片段并使用它而不是默认值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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

========= 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"/>
Run Code Online (Sandbox Code Playgroud)

========= YourActivity.java

CustomFrameLayout mapRoot = (CustomFrameLayout) findViewById(R.id.map);
mapRoot.setOnDragListener(this);

.......
@Override
public void onDrag() {
    //reset your flag here
}
Run Code Online (Sandbox Code Playgroud)

旁注:你知道你的android:name="com.google.android.gms.maps.SupportMapFragment"属性FrameLayout是无用的......对吧?您通常android:name<fragment>xml元素上指定属性.不是布局.