2

我正在使用此代码创建拖放:

private final class MyTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
      if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        ClipData data = ClipData.newPlainText("", "");

        GlobalTouchX = (int) motionEvent.getX();
        GlobalTouchY = (int) motionEvent.getY();

        shadowBuilder = new MyDragShadowBuilder(view);       

        view.startDrag(data, shadowBuilder, null, 0);

        CurrentDragImage = (ImageView)view;
        view.setVisibility(View.GONE);

        return true;
      } else {
      return false;
      }
    }
  }

如何在不等待放置的情况下中断拖动方法,或者我可以以编程方式调用放置事件?我尝试了很多方法,但没有运气。例如,如果我可以在这里打断它,那就太好了:

MainRelative.setOnDragListener(new OnDragListener() {                  
  public boolean onDrag(View v, DragEvent event) {

  int action = event.getAction();

  switch (event.getAction()) {

  case DragEvent.ACTION_DRAG_LOCATION:    

        //!!!!!!!!!!!!!!!
        //HERE I WANT TO INTERRUPT DRAG EVENT ON SOME CONDITION
        //!!!!!!!!!!!!!!!

       break;

  case DragEvent.ACTION_DROP:

       MyOnDrop(v, event, true);                  

       break;
  }

return true;}
});
4

1 回答 1

0

拦截运动的一种方法是覆盖 ViewGroup 类中的 onInterceptTouchEvent(MotionEvent me) 方法,如果您拖动到视图上,则会调用此方法,因此如果您需要拦截,则必须将逻辑放在此方法中.

例如,如果您想使用 FrameLayout 来保存您的子视图。然后,您必须创建一个扩展此 FrameLayout 类的新类并覆盖 onInterceptTouchEvent(MotionEvent me) 方法:

class NewFrameLayout extends FrameLayout{


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

     ///Place your logic here to intercept the dragging event


        /*
         * This method JUST determines whether we want to intercept the motion.
         * If we return true, onMotionEvent will be called and we do the actual
         * work here.
         */

        /*
        * Shortcut the most recurring case: the user is in the dragging
        * state and he is moving his finger.  We want to intercept this
        * motion.
        */
    }
}
于 2013-03-05T17:25:02.040 回答