onFling conflict with onSingleTapUp in Gesturedetector

Asked

Viewed 55 times

2

I have an application running on Android with a Gridview (grid).

On this grid I need to detect various events and so I’m using a gesture detector. Only by clicking on a grid item, sometimes the onFling instead of onSingleTapUp.

Is the behavior really like that or am I doing something wrong? If that’s how I can get around the problem?

class DetectorGestosGrelha extends SimpleOnGestureListener 
{
        /**
         * Para passar de uma grelha para outra ao deslizar o dedo
         * da direita para a esquerda.
         */
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, 
                              float velocityX,float velocityY) 
        {
            //meu código
            return false;    
        }

        /**
         * Para ir para outra atividade ao clicar num elemento da grelha.
         */
        @Override
        public boolean onSingleTapUp(MotionEvent e) 
        {
            //meu código
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) 
        {
            //meu código
            super.onLongPress(e);
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                               float distanceX, float distanceY) 
        {
            //meu código
            return true;
        }

        @Override
        public void onShowPress(MotionEvent e) 
        {
            super.onShowPress(e);
        }

        @Override
        public boolean onDown(MotionEvent e) 
        {
            //meu código
            return true;
        }
}
  • Are you applying some "filter" to accept "Esture" as "Fling"? That is, you should only accept a "Esture" as "Fling" if it has a certain amplitude and speed.

  • @I am but it is inside the onFling.

  • Yes it would be there. Try to increase the values.

  • @ramaral this is already right, I did not explain well then. It will stop at the onFling but does nothing, but then does not pass in the onSingleTapUp and does not do what I want is to go to another activity.

  • The next suggestion would be to return the method false, but you already do. I can’t think of anything else.

  • The difficulty is in the problem not always occur, however try to put return false in the method onDown().

  • See the class Gestureeventlogger.java of this reply and put your Return equal.

  • The problem seems to be the sensitivity to touch, if I put the tablet lying down and use a pen own works well, but as it is a software for a restaurant has to be fast and that is why it gives this problem.

Show 5 more comments

1 answer

2


I solved the problem with a kind of gambiarra:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, 
                       float velocityX,float velocityY) {

    int pos  = pointToPosition((int) e1.getX(), (int) e1.getY());

    int pos2 = pointToPosition((int) e2.getX(), (int) e2.getY());

    if( pos == pos2 ) { 
         //executa o código do click
    }
    else {
         //age com o onFling normal.
    }
    return true;    
}

That is, if the position of the onFling initial is equal to the final, same object, then it is a click, otherwise it makes the normal "slidings".

  • It shouldn’t be necessary to do this! That is, if the two positions are equal, the onFling() should not be called.

  • @but the truth is that they are. Only if I missed something...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.