按 Activity 滑动 运行 执行 OnBackPress
Swipe run by Activity Do OnBackPress
它可以捕获从左向右滑动的动作和 运行 activity 的命令?
在 activity 的情况下将识别滑动(从左到右)和 运行 命令 onBackPress();
我需要在视图中识别滑动,我在 activity 中持有其他组件,但不知道如何识别该操作是 activity 还是组件。
使用以下代码,它可以有一个响应后退手势的侦听器。 View setOnTouchListener 可以识别移动并执行操作。
执行
android.view.GestureDetector gestureDetector = new android.view.GestureDetector(this, new GestureDetector(Activity.this));
View.OnTouchListener gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
view.setOnTouchListener(gestureListener);
GestureDetector.java
public class GestureDetector extends android.view.GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private final Activity activity;
public GestureDetector(Activity activity){
this.activity = activity;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
activity.onBackPressed();
}
} catch (Exception e) {
// nothing
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
}
它可以捕获从左向右滑动的动作和 运行 activity 的命令? 在 activity 的情况下将识别滑动(从左到右)和 运行 命令 onBackPress(); 我需要在视图中识别滑动,我在 activity 中持有其他组件,但不知道如何识别该操作是 activity 还是组件。
使用以下代码,它可以有一个响应后退手势的侦听器。 View setOnTouchListener 可以识别移动并执行操作。
执行
android.view.GestureDetector gestureDetector = new android.view.GestureDetector(this, new GestureDetector(Activity.this));
View.OnTouchListener gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
view.setOnTouchListener(gestureListener);
GestureDetector.java
public class GestureDetector extends android.view.GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private final Activity activity;
public GestureDetector(Activity activity){
this.activity = activity;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
activity.onBackPressed();
}
} catch (Exception e) {
// nothing
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
}