Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.view.GestureDetector;
- import android.view.MotionEvent;
- import android.view.View;
- public class OnSwipeTouchListener implements View.OnTouchListener {
- private final GestureDetector gd = new GestureDetector(new GestureListener());
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- return gd.onTouchEvent(event);
- }
- private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
- private static final int SWIPE_THRESHOLD = 100;
- private static final int SWIPE_VELOCITY_THRESHOLD = 100;
- @Override
- public boolean onDown(MotionEvent e) {
- return true;
- }
- @Override
- public boolean onSingleTapConfirmed(MotionEvent e) {
- onClick();
- return true;
- }
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
- float diffX = e2.getX() - e1.getX();
- try {
- if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
- if (diffX > 0) {
- onSwipeRight();
- } else {
- onSwipeLeft();
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return true;
- }
- }
- public void onSwipeRight() {}
- public void onSwipeLeft() {}
- public void onClick() {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement