OnSwipeTouchListener.java 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package de.tudarmstadt.informatik.hostage.ui2.listeners;
  2. import android.view.GestureDetector;
  3. import android.view.GestureDetector.SimpleOnGestureListener;
  4. import android.view.MotionEvent;
  5. import android.view.View;
  6. import android.view.View.OnTouchListener;
  7. import de.tudarmstadt.informatik.hostage.ui.MainActivity;
  8. /**
  9. * @author Alexander Brakowski
  10. * @created 27.01.14 23:47
  11. */
  12. public class OnSwipeTouchListener implements OnTouchListener {
  13. private final GestureDetector gestureDetector = new GestureDetector(MainActivity.getContext(), new GestureListener());
  14. public boolean onTouch(final View view, final MotionEvent motionEvent) {
  15. return gestureDetector.onTouchEvent(motionEvent);
  16. }
  17. private final class GestureListener extends SimpleOnGestureListener {
  18. private static final int SWIPE_THRESHOLD = 120;
  19. private static final int SWIPE_VELOCITY_THRESHOLD = 150;
  20. @Override
  21. public boolean onDown(MotionEvent e) {
  22. return true;
  23. }
  24. @Override
  25. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  26. try {
  27. float diffX = e2.getX() - e1.getX();
  28. if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
  29. if (diffX > 0) {
  30. onSwipeRight();
  31. } else {
  32. onSwipeLeft();
  33. }
  34. }
  35. } catch (Exception exception) {
  36. exception.printStackTrace();
  37. return false;
  38. }
  39. return true;
  40. }
  41. }
  42. public void onSwipeRight() {
  43. }
  44. public void onSwipeLeft() {
  45. }
  46. }