How to cancel View.OnDragListener in android when dragging and dropping an operation?

Im using view.startDrag() to start a drag and drop operation. When dragging, events are received in OnDragListener . Also im using DragShadowBuilder to show the last View B image when dragging.

Is there a way to stop or cancel or interrupt the drag from some external events or operations? for example, there are 2 views, View A and View B , I drag View B over View A When dragging due to some external event or operation, I want to cancel the drag operations or cancel OnDragListener (without removing my finger from 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; } } 
+6
source share
1 answer

I could not find any trick for this. I tried to send the ACTION_UP motion event, and it did nothing. As long as the user's finger is held pressed, there is no interruption in the hierarchy of events. I did not find anything to help with google search.

I just set the globality and waited until the drag and drop was done, then continue.

0
source

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


All Articles