0
我想先從刷卡向左或向右的手勢在活動的活動,用下面的代碼:禁用上下一扔滑動手勢與GestureDetectorCompat
gestureDetector = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener(){
private static final int SWIPE_THRESHOLD = 50;
private static final int SWIPE_VELOCITY_THRESHOLD = 0;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
//user will move forward through messages on fling up or left
boolean forward = false;
//user will move backward through messages on fling down or right
boolean backward = false;
//calculate the change in X position within the fling gesture
float horizontalDiff = event2.getX() - event1.getX();
//calculate the change in Y position within the fling gesture
float verticalDiff = event2.getY() - event1.getY();
float absHDiff = Math.abs(horizontalDiff);
float absVDiff = Math.abs(verticalDiff);
float absVelocityX = Math.abs(velocityX);
float absVelocityY = Math.abs(velocityY);
if(absHDiff > absVDiff && absHDiff > SWIPE_THRESHOLD && absVelocityX > SWIPE_VELOCITY_THRESHOLD){
//move forward or backward
if(horizontalDiff>0) backward=true;
else forward=true;
}
else if(absVDiff > SWIPE_THRESHOLD && absVelocityY > SWIPE_VELOCITY_THRESHOLD){
if(verticalDiff>0) backward=true;
else forward=true;
}
//user is cycling forward through messages
if(forward){
//check current message is not at end of array, increment or set back to start
swipeTo(SWIPE_LEFT);
}
//user is cycling backwards through messages
else if(backward){
//check that current message is not at start of array, decrement or set to last message
swipeTo(SWIPE_RIGHT);
}
//return super.onFling(event1, event2, velocityX, velocityY);
return true;
}
});
如何禁用從動作當滑動垂直時(從上到下或從下往上)時,會採取什麼措施?
SWIPE_LEFT和SWIPE_RIGHT只是分別具有值1和2的類成員常量。
在此先感謝。
謝謝@jaibatrik,但多少「太高」? – Shareb
使用'android.util.Log'打印垂直絕對速度值。嘗試並最終確定適合您的價值。 – jaibatrik