2014-10-07 63 views
0

我正在實施使用內部類SimpleOnGestureListener進行滑動/一次性移動的手勢檢測。然而,沒有檢測到手勢。未檢測到一次性/滑動手勢

該XML佈局有一個父母ScrollView所以我想知道這是否是一個潛在的原因。

onCreate我初始化GestureListener

public class ViewSelectedDive extends Activity implements OnClickListener, 
     OnRatingBarChangeListener {... 

public GestureDetectorCompat gestureDetectorObject; 

protected void onCreate(Bundle savedInstanceState) {.. 
// instantiate gesture detector 
     gestureDetectorObject = new GestureDetectorCompat(this, new GestureListener()); 

覆蓋的onTouchEvent方法,以確保在檢測到觸摸事件時,手勢監聽器被使用。

@Override 
    public boolean onTouchEvent(MotionEvent event) { 
     // tell teh activity tto use gerture listener when touch event is detected 

     gestureDetectorObject.onTouchEvent(event); 

     Log.d(TAG, "312 ON TOUCH EVENT"); 
     return super.onTouchEvent(event); 
    } 

//內部類來處理guerter檢測

public class GestureListener extends GestureDetector.SimpleOnGestureListener{ 

     private float flingMin=100; 
     private float velocityMin = 100; 



     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
       float velocityY) { 
      // TODO Auto-generated method stub 


      Log.d(TAG, "ON FLING INNER CLASS 951"); 
      // user move to next dive in db 
      boolean moveToNextDive=false; 
      boolean moveToPreviousDive=false; 

      // caulate the change in x pos within fling ges 
      float horizontalDiff = e2.getX()-e1.getX(); 
      float verticalDiff = e2.getY() - e1.getY(); 

      // calulate the abs values 
      float absHDiff = Math.abs(horizontalDiff); 
      float absVDiff = Math.abs(verticalDiff); 
      float absVelocityX = Math.abs(velocityX); 
      float absVelocityY = Math.abs(velocityY); 

        // now of hor distance > vertival distance , move back or forward 
        if(absHDiff>absVDiff && absHDiff>flingMin && absVelocityX> velocityMin){ 

         // swiping forwad 
         if(horizontalDiff>0) { 
          moveToPreviousDive=true; 

          Toast.makeText(getApplicationContext(), "BACKWARD GESTURE DETECTED", Toast.LENGTH_LONG).show(); 
          Log.d(TAG, "SWIPE HORIZONTAL DETECTED BACKWARD"); 

         }else{ 
          moveToNextDive=true; 
           Toast.makeText(getApplicationContext(), "FORWARD GESTURE DETECTED", Toast.LENGTH_LONG).show(); 
           Log.d(TAG, "SWIPE HORIZONTAL FORWAD DETECTED BACKWARD"); 
         } 

        }// outer ifelse if(absVDiff>flingMin && absVelocityY>velocityMin){ 

        else if(absVDiff>flingMin && absVelocityY>velocityMin){ 

         // vertical swipe detected 

          if(verticalDiff>0) { 
           moveToPreviousDive=true; 

           Toast.makeText(getApplicationContext(), "VERTICAL BACKWARD GESTURE DETECTED", Toast.LENGTH_LONG).show(); 
           Log.d(TAG, "SWIPE VERTICAL DETECTED BACKWARD"); 
          } 


          else{ 
           moveToNextDive=true; 

           Log.d(TAG, "SWIPE VERTICAL FORWARD DETECTED BACKWARD"); 
           Toast.makeText(getApplicationContext(), "VERTICAL FORWARD GESTURE DETECTED", Toast.LENGTH_LONG).show(); 
          } 
         } 

      return true; 
     } 

     @Override 
     public boolean onDown(MotionEvent e) { 

//   Returning true tells the operating system that your code 
//   is interested in the remaining gesture events. 
      Log.d(TAG, "ON DOWN INNER CLASS 1010"); 
      return true; 
     } 

回答

0

得到它,滾動視圖,所以你需要通過覆蓋活動,他們派遣到姿態探測器對象將註冊所有觸摸事件,disptachTouchEvent ...希望這有助於任何人在相同的情況..

@Override 
    public boolean dispatchTouchEvent(MotionEvent ev) { 
     // must use this to pass event on to Gesture object from scrollvoew n xml 

     super.dispatchTouchEvent(ev); 

     return this.gestureDetectorObject.onTouchEvent(ev); 
    }