0

I need to catch the gesture motion from right to left and vice versa (swipe) Why is it not working?

public boolean onTouchEvent(MotionEvent event)
    {

        float x = event.getX();
        float y = event.getY();
        int action = event.getAction();
        int edgeFlags = event.getEdgeFlags();


        switch (edgeFlags)

        {
            case MotionEvent.EDGE_LEFT:
            sex=3;   break;


            case MotionEvent.EDGE_RIGHT:
            sex=1;
                break;

            case MotionEvent.EDGE_TOP:
                break;

            case MotionEvent.EDGE_BOTTOM:
                break;

            default:

                break;
        }

return true;
}

it does not suit me -> How to detect the swipe left or Right in Android?

Community
  • 1
  • 1
user2812337
  • 29
  • 1
  • 6

2 Answers2

0

Try with this approach:

public abstract class CustomGestureListener extends GestureDetector.SimpleOnGestureListener implements Loggable{

private final View mView;

public CustomGestureListener(View view){
    mView = view;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
    mView.onTouchEvent(e);
    return super.onSingleTapConfirmed(e);
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    onTouch();
    return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    if (e1.getX() < e2.getX()) {
        return onSwipeRight();
    }

    if (e1.getX() > e2.getX()) {
        return onSwipeLeft();
    }

    return onTouch();
}

public abstract boolean onSwipeRight();
public abstract boolean onSwipeLeft();
public abstract boolean onTouch();

}

Check this article for the full source example.

Eury Pérez Beltré
  • 2,017
  • 20
  • 28
  • One problem with this is that fling is very sensitive- its possible to detect one when lifting your finger without swiping. Its based on speed of change. I would also include a magnitude of change, to make sure a small movement doesn't turn into a fling. – Gabe Sechan Mar 21 '16 at 15:58
  • You aren't all people, and touch events can be sensitive to size and shape of fingers. I get fling events 20% of the time when I lift a finger. Its not a bad starting approach, it just needs additions. – Gabe Sechan Mar 21 '16 at 16:01
0

Because edge flags are only returned if the user takes their thumb all the way off the edge of the screen, which is a rarity. You need to dump this approach. Instead capture the point where they set their finger down, save the location and the time. When you get the matching up event, compare the location and time and make sure it was a mostly horizontal move and in a quick enough time frame. That would be a swipe.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127