2016-12-13 15 views
0

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

switch(event.getAction()) { 

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

变化是指在运行时的背景。 – Vinodh

+0

您正在使用** Listview **或** Recyclerview **? – Lovekesh

回答

0

你可以在你的类/片段中实现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

+0

雅我使用相同的。但我也使用案例MotionEvent.ACTION_MOVE来跟踪移动。所以在某些Action_move情况下,我想引发DragEvent.ACTION_DROP。 – android

+0

请参考https://developer.android.com/guide/topics/ui/drag-drop.html – sasikumar