您可以轻松地使用手势检测器创建一个扩展类,该类SimpleOnGestureListener
具有以下onFling
方法:
protected class ExampleGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {// User swipe vertically
}
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_MIN_VELOCITY) {// Right swipe
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_MIN_VELOCITY) {// Left swipe
}
return false;
}
}
你只需要根据需要定义变量,创建你的gestureDetector
gestureDetector = new GestureDetector(new ExampleGestureDetector());
并为您想要的视图重新定义触摸侦听器
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View wv, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
编辑:经过测试,它似乎只适用于可滚动项目。