使用 GestureDetectorCompat 禁用上下滑动手势
Disable up and down fling swipe gestures with GestureDetectorCompat
我正在尝试通过在 activity 中向右或向左滑动手势来启动 activity,代码如下:
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只是class成员常量,其值分别为1和2。
提前致谢。
我认为您应该检测 absVelocityY
的高值。如果该值太高,则表示垂直投射。在这些情况下,不要开始你的新 Activity
.
我正在尝试通过在 activity 中向右或向左滑动手势来启动 activity,代码如下:
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只是class成员常量,其值分别为1和2。
提前致谢。
我认为您应该检测 absVelocityY
的高值。如果该值太高,则表示垂直投射。在这些情况下,不要开始你的新 Activity
.