GestureDetector.OnGestureListener:用来通知普通的手势事件,该接口有如下六个回调函数:
1. onDown(MotionEvent e):down事件;
2. onSingleTapUp(MotionEvent e):一次点击up事件;
3. onShowPress(MotionEvent e):down事件发生而move或则up还没发生前触发该事件;
4. onLongPress(MotionEvent e):长按事件;
5. onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY):滑动手势事件;
6. onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY):在屏幕上拖动事件。
主要判断是在onFling()函数里面,e1表示开始按下去的位置信息,e2表示抬起时的位置信息,因此可以通过它们在x轴上面 的距离差来是左滑还是右滑。。。
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-generated method stub if (e2.getX()-e1.getX() > 100) { // fling right showNextView(); } else if (e1.getX() - e2.getX() > 100) { // fling left showPreviousView(); } return false; }