OnDragListener未收到DRAG_STARTED或DRAG_ENDED,但确实收到ACTION_DROP [英] OnDragListener not receiving DRAG_STARTED or DRAG_ENDED, but does get ACTION_DROP

查看:275
本文介绍了OnDragListener未收到DRAG_STARTED或DRAG_ENDED,但确实收到ACTION_DROP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OnDragListener 遇到了一个奇怪的问题。我的目标视图可以很好地处理 ACTION_DROP 事件并进行处理;但它永远不会收到 ACTION_DRAG_STARTED ACTION_DRAG_ENDED 事件(实际上,它除了丢弃之外从未收到任何事件)。

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.

我的可拖动视图具有此 OnTouchListener

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;
}

目标视图具有此 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;
}


推荐答案

我的解决方案是以下解决方法:当我的 OnTouchListener 获取ACTION_UP时,设置一个1秒钟的延迟运行,以检查视图是否仍在拖动。如果 OnDragListener 收到了该丢弃,则runnable不执行任何操作;但是如果不是可运行的调用,则停止拖动以进行清理并将视图重置为其先前位置。

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;

这篇关于OnDragListener未收到DRAG_STARTED或DRAG_ENDED,但确实收到ACTION_DROP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆