【问题标题】:How to set Action in DragEvent如何在 DragEvent 中设置动作
【发布时间】:2016-12-13 04:20:25
【问题描述】:

我正在使用OnDragListener 拖放图像。我有MOTIONEVENT.ACTION_MOVE 来实现图像移动功能。在动作移动的某个时刻,我想结束拖动并移除它的阴影。是否可以在拖动事件中设置动作?在释放手指之前,我想调用 drop 事件。

switch(event.getAction()) {

      case MotionEvent.ACTION_MOVE:
           //here i want to remove shadow and stop dragging
           break;
      case DragEvent.ACTION_DROP:break;
   }

【问题讨论】:

  • 您使用的是 Listview 还是 Recyclerview
  • 在运行时相应地改变背景。

标签: android drag-and-drop touch motionevent


【解决方案1】:

您可以在您的类/片段中实现 OnDragListener。

          class MyDrag implements OnDragListener {
            Drawable image = getResources().getDrawable(
                            R.drawable.shape_droptarget);

            @Override
            public boolean onDrag(View v, DragEvent event) {
                    int action = event.getAction();
                    switch (event.getAction()) {
                    case DragEvent.ACTION_DRAG_STARTED:
                            // Signals the start of a drag and drop operation
                            break;
                    case DragEvent.ACTION_DRAG_ENTERED:
                           //Signals to a View that the drag point has entered the bounding box of the View
                            v.setBackgroundDrawable(image);
                            break;
                    case DragEvent.ACTION_DRAG_EXITED:
                           //Signals that the user has moved the drag shadow out of the bounding box of the View or into a descendant view that can accept the data.
                            v.setBackgroundDrawable(image);
                            break;
                    case DragEvent.ACTION_DROP:
                            // Signals to a View that the user has released the drag shadow, and the drag point is within the bounding box of the View and not within a descendant view that can accept the data.
                            View view = (View) event.getLocalState();
                            ViewGroup owner = (ViewGroup) view.getParent();
                            owner.removeView(view);
                            LinearLayout container = (LinearLayout) v;
                            container.addView(view);
                            view.setVisibility(View.VISIBLE);
                            break;
                    case DragEvent.ACTION_DRAG_ENDED:
                          //Signals to a View that the drag and drop operation has concluded.
                            v.setBackgroundDrawable(image);
                    default:
                            break;
                    }
                    return true;
            }
    }

更多详情请参考这里Drag and Drop

【讨论】:

  • 是的,我正在使用相同的。但我也在使用案例 MotionEvent.ACTION_MOVE 来跟踪移动。所以在某些 Action_move 情况下,我想触发 DragEvent.ACTION_DROP。
猜你喜欢
  • 1970-01-01
  • 2014-08-29
  • 1970-01-01
  • 1970-01-01
  • 2013-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多