3

I'm using GestureDetector.OnGestureListener to invoke method when user moves finger across the screen in the specified direction. My problem - just move slightly and the method is called, I would like the user to move his finger a little more on the screen.

public class SwipeListener extends GestureDetector.SimpleOnGestureListener {
     public static final int MIN_SWIPE_DISTANCE = 40; 


    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }


    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
       float xDiff = e1.getX() - e2.getX();
       float yDiff = e1.getY() - e2.getY();
       return resolveSwipe(xDiff, yDiff);
    }


    private boolean resolveSwipe(float xDist, float yDist) {
        float yDistAbs = Math.abs(yDist);
        float xDistAbs = Math.abs(xDist);

        SwipeDirection swipeDirection;


        if (yDistAbs > xDistAbs) {
            if (yDistAbs < MIN_SWIPE_DISTANCE) {return false;}
            swipeDirection = (yDist > 0) ? SwipeDirection.DOWN: SwipeDirection.UP;

        } else {
            if (xDistAbs < MIN_SWIPE_DISTANCE) {return false;}
            swipeDirection = (xDist > 0) ? SwipeDirection.RIGHT: SwipeDirection.LEFT;
        }

        onSwipeEvent(swipeDirection);
        return true;
    }

    private void onSwipeEvent(SwipeDirection swipeDirection) {
        if (swipeDirection == SwipeDirection.UP) {

            return;
        }
        if (swipeDirection == SwipeDirection.DOWN) {

            return;
        }
        if (swipeDirection == SwipeDirection.LEFT) {
         finger1()
            return;
        }
        if (swipeDirection == SwipeDirection.RIGHT) {
            finger1();
            return;
        }

}

public enum SwipeDirection {
    UP, DOWN, LEFT, RIGHT
}

Is it possible? What should I change or add here?

Pochmurnik
  • 780
  • 6
  • 18
  • 35

2 Answers2

0

You need to calculate as to what level of x-axis or y-axis you don't want to perform any action on swipe.

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
0

You have to declare minimum distance for swipe as private static final int SWIPE_MIN_DISTANCE = 160 may be it will help you Click here to implement it

Mohammad Arman
  • 508
  • 4
  • 13
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/16892191) – jwenting Aug 01 '17 at 07:05
  • @jwenting Thanks for suggesting – Mohammad Arman Aug 01 '17 at 07:11