How to cancel View.OnDragListener in android during dragging Operation?
Asked Answered
G

2

7

Im using view.startDrag() to start dragging operation. While dragging, events are received in OnDragListener. Also im using DragShadowBuilder to show the last image of View B while dragging.

Is there a way to stop or cancel or abort dragging from some external events or operation? for example, there are 2 view, View A and View B, i'm dragging View B over View A. While dragging, due to some external event or operation, i want to cancel dragging operations or cancel OnDragListener (without removing my finger from the view B).

Code Snippet for DragShadowBuilder:

DragShadowBuilder shadowBuilder = new DragShadowBuilder(view) {
    @Override
    public void onDrawShadow(Canvas canvas) {
        canvas.drawBitmap(b, 0, 0, new Paint());
        super.onDrawShadow(canvas);
    }
};
boolean dragSuccess = false;
dragSuccess = view.startDrag(null, shadowBuilder, view, 0);

Code Snippet for OnDragListener:

private final class ViewDragListener implements OnDragListener {
    @Override
    public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                 log("ACTION_DRAG_STARTED");
                 break;
            case DragEvent.ACTION_DRAG_ENTERED:
                 log("ACTION_DRAG_ENTERED");
                 break;
            case DragEvent.ACTION_DRAG_EXITED:
                 log("ACTION_DRAG_EXITED");
            case DragEvent.ACTION_DROP:
                 log("ACTION_DROP");
                 mX = (int) event.getX();
                 mY = (int)event.getY();
                 break;
            case DragEvent.ACTION_DRAG_ENDED:
                 log("ACTION_DRAG_ENDED");
                 //Doing some cleanup operations.
                 break;
            case DragEvent.ACTION_DRAG_LOCATION:
                 log("ACTION_DRAG_LOCATION");
                 break;
            default:
                 break;
        }
        return true;
    }
}
Graphophone answered 10/6, 2014 at 12:15 Comment(0)
C
1

I couldn't find any trick for this. I tried sending a motion event of ACTION_UP and it did nothing. As long as the user's finger is held down, there's no interrupting the event hierarchy, it seems. I found nothing to help with a google search.

I just set a global and waited until the drag ended, then proceeded.

Chess answered 21/3, 2015 at 1:13 Comment(0)
H
0

Is this what you are looking for:

https://developer.android.com/reference/android/view/View.html#cancelDragAndDrop()

?

cancelDragAndDrop Added in API level 24

public final void cancelDragAndDrop () Cancels an ongoing drag and drop operation.

A DragEvent object with DragEvent.getAction() value of DragEvent.ACTION_DRAG_ENDED and DragEvent.getResult() value of false will be sent to every View that received DragEvent.ACTION_DRAG_STARTED even if they are not currently visible.

This method can be called on any View in the same window as the View on which startDragAndDrop was called.

Hyponasty answered 19/2, 2024 at 8:5 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.