Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnDragListener not receiving DRAG_STARTED or DRAG_ENDED, but does get ACTION_DROP

I'm running into a weird problem with an OnDragListener. My target view gets the ACTION_DROP event fine and handles it; but it never receives the ACTION_DRAG_STARTED or ACTION_DRAG_ENDED events (in fact it never receives any events besides drop).

What could be causing this? It's an issue because I can't handle the case when the drop happens outside of the target.

I found this question but the answer was not clear to me. Any ideas greatly appreciated.

My draggable view has this OnTouchListener:

@Override
public boolean onTouch(View v, MotionEvent ev) {
  switch (ev.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
      startPointX = ev.getX();
      startPointY = ev.getY();
      isOnClick = true;
      break;
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
      if (isOnClick) {
        isOnClick = false;
        // handle single click
      }
      break;
    case MotionEvent.ACTION_MOVE:
      if (isOnClick && movePassesThreshold(ev)) {
        isOnClick = false;
        draggableView.startDrag(...);
      }
      break;
    default:
      break;
  }
  return true;
}

And the target view has this OnDragListener:

@Override
public boolean onDrag(View v, DragEvent event) {
  switch (event.getAction()) {
    case DragEvent.ACTION_DRAG_STARTED:
      Log.v(TAG, "drag started");
      break;
    case DragEvent.ACTION_DRAG_ENTERED:
      break;
    case DragEvent.ACTION_DRAG_EXITED:
      break;
    case DragEvent.ACTION_DROP:
      Log.v(TAG, "drop");
      // handle drop
      break;
    case DragEvent.ACTION_DRAG_ENDED:
      Log.v(TAG, "drag ended");
      break;
    default:
      return false;
  }
  return true;
}
like image 613
mariaines Avatar asked Oct 16 '13 18:10

mariaines


2 Answers

This is a known issue with the ViewGroup:

https://code.google.com/p/android/issues/detail?id=25073

Overriding the dispatchDragEvent function as suggested at that link worked for me:

@Override
public boolean dispatchDragEvent(DragEvent ev){
    boolean r = super.dispatchDragEvent(ev);
    if (r && (ev.getAction() == DragEvent.ACTION_DRAG_STARTED
            || ev.getAction() == DragEvent.ACTION_DRAG_ENDED)){
        // If we got a start or end and the return value is true, our
        // onDragEvent wasn't called by ViewGroup.dispatchDragEvent
        // So we do it here.
        onDragEvent(ev);
    }
    return r;
}
like image 105
Mike Avatar answered Nov 11 '22 18:11

Mike


My solution is this workaround: when my OnTouchListener gets ACTION_UP, set a 1-second delayed runnable to check if the view is still dragging. If the OnDragListener received the drop, the runnable does nothing; but if it didn't the runnable calls stop drag to clean up and reset the view to its previous position.

case MotionEvent.ACTION_UP:
  if (isOnClick) {
    isOnClick = false;
    // handle single click
  } else if (draggableView.isDragging()) {
    uiHandler.postDelayed(new Runnable() {
      @Override
      public void run() {
        if (draggableView.isDragging()) {
          // Drop never got received, so call stopDrag
          draggableView.stopDrag();
        }
      }
    }, 1000);
  }
  break;
like image 32
mariaines Avatar answered Nov 11 '22 17:11

mariaines