Drag and drop detection on Android Google Maps

I am developing an application using the Google Maps API. I have a boolean that tells me whether I am following the user's movement or not. I want to put it in false when the user drags the map. But how can I do this? What is my code

<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"/>

and in java class i call it

    mapFragment = new MapFragment();
    getFragmentManager().beginTransaction().add(R.id.map, mapFragment).commit();
    getFragmentManager().executePendingTransactions();
+4
source share
1 answer

You can create a custom root layout that spies on touch events descending to a map fragment and use it instead of the standard one 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 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. .

+8

Source: https://habr.com/ru/post/1589467/


All Articles