android – Can I prevent onFling() and onDown() from both firing?-ThrowExceptions

Exception or error:

I’m detecting both the onDown() and onFling() gestures for two different purposes, both work.

Within onDown() a sound is to be played, while during onFling() a swipe is detected and a swipe related action is taken – no sound should be played during the onFling() gesture.

The problem is that the onDown() sound is being fired when I perform a swiping motion or when onFling() is fired.

My current onDown() gesture looks like:

@Override
public boolean onDown(MotionEvent e) {

    playSound();

    return true;
}

My onFling() looks like:

@Override
public boolean onFling(MotionEvent downEvent, MotionEvent moveEvent, float velocityX, float velocityY) {
    boolean result = false;

    float diffY = moveEvent.getY() - downEvent.getY();
    float diffX = moveEvent.getX() - downEvent.getX();

    // which was greater? movement across Y or X
    if( Math.abs(diffX) > Math.abs(diffY) ) {

        // right or left swipe
        if( Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {

            doSomethingCool();

            result = true;

        }
    } else {
        // up or down swipe
        if( Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {

            doSomethingCool();

            result = true;
        }
    }

    return result;
}

I’ve checked the GestureDetector class to see if onDown() could have multiple parameters like onDown(MotionEvent downEvent, MotionEvent moveEvent) but that isn’t possible.

I’ve tried using MotionEvent constants (both ACTION_DOWN and ACTION_MOVE) within onDown() to eliminate swipes/flings as a possible option:

@Override
public boolean onDown(MotionEvent e) {

    if(e.getAction() == e.ACTION_DOWN) {
        playSound();
        return true;
    }

    return false;
}

However that hasn’t worked. Is this something that is just unavoidable?

I understand that in order to perform a “flinging” motion, you must first put your finger down… but I’m hoping there’s a way to double check that a users finger is down and NOT actually in motion (swiping/flinging).

Thanks in advance!

How to solve:

Using onSingleTapUp and onDoubleTap replce onDown.

I think it can avoid trigger onFling.

Leave a Reply

Your email address will not be published. Required fields are marked *