Cancel swipe navigaton

146 Views Asked by At

I'm trying to cancel card swipe in glass if card has not been processed. I have special indicator which is checked in SWIPE_RIGHT

gestureDetector.setBaseListener(new GestureDetector.BaseListener() {

    @Override
    public boolean onGesture(Gesture gesture) {

        if (gesture == Gesture.TAP) {

            openOptionsMenu();
            return true;
        } else if (gesture == Gesture.TWO_TAP) {

            runSpeechRecognition();
            return true;
        } else if (gesture == Gesture.SWIPE_RIGHT) {

            // do something on right (forward) swipe
            CheckCards checkCard = mCheckCardsInfo.get(cardPosition);
            if (checkCard.getStatus() == 1){

                return true;
            }else{

                return false;
            }
        } else if (gesture == Gesture.SWIPE_LEFT) {

            // do something on left (backwards) swipe
            return true;
        } else if (gesture == Gesture.SWIPE_DOWN) {

            finish();
        }
        return false;
    }
});

I thought that returning false is enought (I chcked in debugger and it's called), but even then card is changend. I tried to find any method, but unsuccessfully. Maybe I missed something.

1

There are 1 best solutions below

0
On

You're error is very simple! You say, "I thought that returning false is enough" - if I understand you correctly, you want to cancel the swipe navigation. Basically, stop the swipe navigation from occurring.

You just have it backwards. From the Google Developers page on GestureDetectors:

public boolean onMotionEvent (MotionEvent event)

Added in API level XE12 Processes a motion event, returning true if events should always be consumed or if a gesture was detected.

Returns:

reflects whether touch event is consumed

The meaning of the return value is the same for all of the different methods. You return true to consume the event, and false to not consume the event. In other words, if you want to be the only one to handle the event, and not allow the default actions to occur on the event, you return true to indicate that you consume the event.

To fix your problem, just return true, not false!