2014-03-26 114 views
2

我用onDragListener在屏幕上拖動一個對象,這部分工作正常。但我也需要在拖動時檢查屏幕的x,y座標。我重寫onTouch方法,它也可以很好地工作。但是一旦我拖動ontouch偵聽器的對象不起作用。我不能讓這兩個聽衆一起工作。我不是爲什麼它不能一起工作。如何使onDragListener和onTouchListener一起工作?

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

回答

2

把(下同)draglistener也填滿你的整個畫面的圖下面的代碼,不說電話ACTION_DRAG_LOCATION無處不在? (在這種情況下你不需要觸摸監聽器)