简体   繁体   中英

Android gesture from right to left

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM