Guest User

Untitled

a guest
Mar 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. /**
  2. * Detects left and right swipes across a view.
  3. */
  4. public class OnSwipeTouchListener implements OnTouchListener {
  5.  
  6. private final GestureDetector gestureDetector;
  7.  
  8. public OnSwipeTouchListener(Context context) {
  9. gestureDetector = new GestureDetector(context, new GestureListener());
  10. }
  11.  
  12. public void onSwipeLeft() {
  13. }
  14.  
  15. public void onSwipeRight() {
  16. }
  17.  
  18. public boolean onTouch(View v, MotionEvent event) {
  19. return gestureDetector.onTouchEvent(event);
  20. }
  21.  
  22. private final class GestureListener extends SimpleOnGestureListener {
  23.  
  24. private static final int SWIPE_DISTANCE_THRESHOLD = 100;
  25. private static final int SWIPE_VELOCITY_THRESHOLD = 100;
  26.  
  27. @Override
  28. public boolean onDown(MotionEvent e) {
  29. return true;
  30. }
  31.  
  32. @Override
  33. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  34. float distanceX = e2.getX() - e1.getX();
  35. float distanceY = e2.getY() - e1.getY();
  36. if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
  37. if (distanceX > 0)
  38. onSwipeRight();
  39. else
  40. onSwipeLeft();
  41. return true;
  42. }
  43. return false;
  44. }
  45. }
  46. }
Add Comment
Please, Sign In to add comment