如何使onDragListener和onTouchListener一起工作?

时间:2014-03-26 14:47:36

标签: android ontouchlistener

我使用onDragListener在屏幕上拖动一个对象,这部分效果很好。但我还需要在拖动时检查屏幕的x,y坐标。我重写onTouch方法,它也很好用。但是,一旦我拖动对象,ontouch听众就无法工作。我不能让两个听众一起工作。我不知道它为什么不能一起工作。

以下draginng代码

ima.setOnLongClickListener(new View.OnLongClickListener() {
         @Override
         public boolean onLongClick(View v) {
            ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
            String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
            ClipData dragData = new ClipData(v.getTag().toString(), 
            mimeTypes, item);

            // Instantiates the drag shadow builder.
            View.DragShadowBuilder myShadow = new DragShadowBuilder(ima);

            // Starts the drag
            v.startDrag(dragData,  // the data to be dragged
            myShadow,  // the drag shadow builder
            null,      // no need to use local data
            0          // flags (not currently used, set to 0)
            );
            return true;
         }
      });

      // Create and set the drag event listener for the View
      ima.setOnDragListener( new OnDragListener(){
         @Override
         public boolean onDrag(View v,  DragEvent event){
           switch(event.getAction())                   
           {
            case DragEvent.ACTION_DRAG_STARTED:
               System.out.println("Action is DragEvent.ACTION_DRAG_STARTED");
               break;
            case DragEvent.ACTION_DRAG_ENTERED:
               Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED");
               break;
            case DragEvent.ACTION_DRAG_EXITED :
               Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED");
               break;
            case DragEvent.ACTION_DRAG_LOCATION  :
               Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION");
               right=ima.getRight();
               left=ima.getLeft();
               top=ima.getTop();
               bottom=ima.getBottom();
               System.out.println("Start Touch "+right+" "+top+" "+left+" "+bottom);
              /*  if(x_cord>left&&y_cord>top&&x_cord<right&&y_cord<bottom){
                   System.out.println("GONE");
                   ima.setVisibility(View.GONE);
               }*/
               break;
            case DragEvent.ACTION_DRAG_ENDED   :
                   System.out.println( "ACTION_DRAG_ENDED event");  
               break;
            case DragEvent.ACTION_DROP:
              Log.d(msg, "ACTION_DROP event");
               break;
            default: break;
            }
            return true;
         }
      });

以下触摸侦听器代码

@Override
  public boolean onTouchEvent(MotionEvent event) {

         final int action = event.getAction();
            switch (action & MotionEvent.ACTION_MASK) {

                case MotionEvent.ACTION_DOWN: {
                    x_cord = (int) event.getX();
                       y_cord = (int) event.getY();
                    String text = "You click at x = " + event.getX() + " and y = " + event.getY();
                    //Toast.makeText(this, text, Toast.LENGTH_LONG).show();
                    System.out.println(text);
                    break;
                }

                case MotionEvent.ACTION_MOVE:{
                    x_cord = (int) event.getX();
                       y_cord = (int) event.getY();
                    String text = "You click at x = " + event.getX() + " and y = " + event.getY();
                   // Toast.makeText(this, text, Toast.LENGTH_LONG).show();
                 System.out.println(text);
                    break;
                }

            }
            return true;
}

1 个答案:

答案 0 :(得分:2)

将(相同的)draglistener放在一个填满整个屏幕的视图上,是不是每次都调用ACTION_DRAG_LOCATION? (在这种情况下你不需要触摸监听器)