2014-04-18 50 views
0

@覆蓋 公共布爾的onTouchEvent(MotionEvent事件){ 如果(gestureDetector.onTouchEvent(事件)){ 返回真; } return super.onTouchEvent(event); } private void onLeftSwipe(){ //做些什麼 System.out.println(「left swipe」); }Swipt探測器手勢機器人

private void onRightSwipe() { 
    // Do something 
     System.out.println("right swipe"); 
    } 

    // Private class for gestures 
    private class SwipeGestureDetector 
      extends SimpleOnGestureListener { 
    // Swipe properties, you can change it to make the swipe 
    // longer or shorter and speed 
    private static final int SWIPE_MIN_DISTANCE = 120; 
    private static final int SWIPE_MAX_OFF_PATH = 200; 
    private static final int SWIPE_THRESHOLD_VELOCITY = 200; 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, 
         float velocityX, float velocityY) { 
     try { 
     float diffAbs = Math.abs(e1.getY() - e2.getY()); 
     float diff = e1.getX() - e2.getX(); 

     if (diffAbs > SWIPE_MAX_OFF_PATH) 
      return false; 

     // Left swipe 
     if (diff > SWIPE_MIN_DISTANCE 
     && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
      SlidingMenuActivity.this.onLeftSwipe(); 

     // Right swipe 
     } else if (-diff > SWIPE_MIN_DISTANCE 
     && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
      SlidingMenuActivity.this.onRightSwipe(); 
     } 
     } catch (Exception e) { 
     Log.e("YourActivity", "Error on gestures"); 
     } 
     return false; 
    } 
    } 

我有,但它不工作,我不能夠檢測向左滑動或right.Have聲明爲private GestureDetector gestureDetector;在我的課堂上,加上我添加的活動的創建 gestureDetector = new GestureDetector( new SwipeGestureDetector());

但它不工作,沒有打印?我使用奇巧的Nexus 4

回答

0

您必須應用你喜歡它的工作對聽者來看,是這樣的:

myView.setOnTouchListener(new SwipeGestureDetector(this).... 
+0

它告訴我把swipgesturedetector投給了ontouchlistener – user3278732

+0

我的整個目標是在整個窗口中啓用左側菜單的滑動。不只是在窗口的最左邊。 – user3278732

0

嘗試更新的代碼

public class OnSwipeTouchListener implements OnTouchListener { 
private final GestureDetector gestureDetector; 
int SWIPE_THRESHOLD = 200; 
int SWIPE_VELOCITY_THRESHOLD = 200; 

public OnSwipeTouchListener(Context context) { 
    gestureDetector = new GestureDetector(context, new CustomGestureListenerClass()); 
} 

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

private final class CustomGestureListenerClass extends SimpleOnGestureListener { 


    @Override 
    public boolean onDown(MotionEvent e) { 
     return false; 
    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent e) { 
     singleClicked(e); 
     return super.onSingleTapUp(e); 
    } 

    @Override 
    public boolean onScroll(MotionEvent startMotionEvent, MotionEvent endMotionEvent, float distanceX, float distanceY) { 
     return super.onScroll(startMotionEvent, endMotionEvent, distanceX, distanceY); 
    } 


    @Override 
    public boolean onFling(MotionEvent startMotionEvent, MotionEvent endMotionEvent, float velocityX, float velocityY) { 

     boolean result = false; 
     try { 
      float diffY = endMotionEvent.getY() - startMotionEvent.getY(); 
      float diffX = endMotionEvent.getX() - startMotionEvent.getX(); 
      if (Math.abs(diffX) > Math.abs(diffY)) { 
       if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
        if (diffX > 0) { 
         onSwipeRight(); 
        } else { 
         onSwipeLeft(); 
        } 
       } 
       result = true; 
      } 
      result = true; 
     } catch (Exception exception) { 
      exception.printStackTrace(); 
     } 
     return result; 
    } 
} 

public void onSwipeRight() { 
    // if you want done some portion before call this method then write here 
} 

public void onSwipeLeft() { 
    // if you want done some portion before call this method then write here 
} 

public void singleClicked(MotionEvent e) { 
} 
}