Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Detects left and right swipes across a view.
- */
- public class OnSwipeTouchListener implements OnTouchListener {
- private final GestureDetector gestureDetector;
- public OnSwipeTouchListener(Context context) {
- gestureDetector = new GestureDetector(context, new GestureListener());
- }
- public void onSwipeLeft() {
- }
- public void onSwipeRight() {
- }
- public boolean onTouch(View v, MotionEvent event) {
- return gestureDetector.onTouchEvent(event);
- }
- private final class GestureListener extends SimpleOnGestureListener {
- private static final int SWIPE_DISTANCE_THRESHOLD = 100;
- private static final int SWIPE_VELOCITY_THRESHOLD = 100;
- @Override
- public boolean onDown(MotionEvent e) {
- return true;
- }
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
- float distanceX = e2.getX() - e1.getX();
- float distanceY = e2.getY() - e1.getY();
- if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
- if (distanceX > 0)
- onSwipeRight();
- else
- onSwipeLeft();
- return true;
- }
- return false;
- }
- }
- }
Add Comment
Please, Sign In to add comment