2014-05-22 76 views
1

我正在使用手勢來捕獲觸摸事件,但它在我長按時觸發兩個事件。 當我按下按鈕時,如何禁用onDown事件? 這是我的代碼。Android手勢檢測器onLongPress和onDown

final GestureDetector gestureDetector = new GestureDetector(
       new GestureDetector.SimpleOnGestureListener() { 
        public void onLongPress(MotionEvent e) { 
         Toast.makeText(mContext, 
           "long" + position + counter++, 
           Toast.LENGTH_SHORT).show(); 
        } 

        public boolean onDown(MotionEvent e) { 
         Toast.makeText(mContext, 
           "touch" + position + counter++, 
           Toast.LENGTH_SHORT).show(); 
         return true; 
        } 
       }); 
     category.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       return gestureDetector.onTouchEvent(event); 
      } 
     }); 

回答

0

你可能想使用abstract boolean onSingleTapUp(MotionEvent e)。 您正在使用的回調public boolean onDown(MotionEvent e)會在所有手勢開始時觸發(手指觸摸屏幕時)。 只有在手勢結束時(鬆開手指時)才能檢測到手勢是單擊,長按還是其他任何手勢。