Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. public abstract class SwipeDismissBaseActivity extends AppCompatActivity {
  2. private static final int SWIPE_MIN_DISTANCE = 120;
  3. private static final int SWIPE_MAX_OFF_PATH = 250;
  4. private static final int SWIPE_THRESHOLD_VELOCITY = 200;
  5. private GestureDetector gestureDetector;
  6.  
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. gestureDetector = new GestureDetector(new SwipeDetector());
  11. }
  12.  
  13. private class SwipeDetector extends GestureDetector.SimpleOnGestureListener {
  14. @Override
  15. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  16.  
  17. // Check movement along the Y-axis. If it exceeds SWIPE_MAX_OFF_PATH,
  18. // then dismiss the swipe.
  19. if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
  20. return false;
  21.  
  22. // Swipe from left to right.
  23. // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE)
  24. // and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
  25. if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
  26. finish();
  27. return true;
  28. }
  29.  
  30. return false;
  31. }
  32. }
  33.  
  34. @Override
  35. public boolean dispatchTouchEvent(MotionEvent ev) {
  36. // TouchEvent dispatcher.
  37. if (gestureDetector != null) {
  38. if (gestureDetector.onTouchEvent(ev))
  39. // If the gestureDetector handles the event, a swipe has been
  40. // executed and no more needs to be done.
  41. return true;
  42. }
  43. return super.dispatchTouchEvent(ev);
  44. }
  45.  
  46. @Override
  47. public boolean onTouchEvent(MotionEvent event) {
  48. return gestureDetector.onTouchEvent(event);
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement