Conflict onFling with onSingleTapUp in GestureDetector

2

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

In this grid I need to detect several events and so I'm using a gesture detector. However, clicking a grid item sometimes causes the onFling event instead of onSingleTapUp to fire.

Is the behavior the same or am I doing something wrong? If so, what can I do to 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;
        }
}
    
asked by anonymous 24.05.2016 / 15:18

1 answer

1

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 initial onFling is equal to the final, same object, then it is a click, otherwise it makes normal "slides."

    
27.06.2016 / 18:39