1 setOnTouchListener(new OnTouchListener() { 2 private float startX, startY, offsetX, offsetY; 3 4 @Override 5 public boolean onTouch(View v, MotionEvent event) { 6 switch (event.getAction()) { 7 case MotionEvent.ACTION_DOWN: 8 startX = event.getX(); 9 startY = event.getY(); 10 break; 11 case MotionEvent.ACTION_UP: 12 offsetX = event.getX() - startX; 13 offsetY = event.getY() - startY; 14 if (Math.abs(offsetX) > Math.abs(offsetY)) { 15 if (offsetX < -5) { // left 16 swipeLeft(); 17 } else if (offsetX > 5) { // right 18 swipeRight(); 19 } 20 } else { 21 if (offsetY < -5) { // up 22 swipeUp(); 23 } else if (offsetY > 5) { // down 24 swipeDown(); 25 } 26 } 27 break; 28 default: 29 break; 30 } 31 return true; 32 } 33 });