0

I have an Android app with a GridView.

In that grid I need to detect many events and I have to use a GestureDetector, but sometimes when I click on a grid item is triggered the event onFling instead of onSingleTapUp.

What I'm doing wrong?

class GestureDetectorGrid extends SimpleOnGestureListener 
{
        /**
         * Sliding from right to the left to move to another grid
         */
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, 
                              float velocityX,float velocityY) 
        {
            //my code
            return false;    
        }

        /**
         * Go to another Activity by clicking on a element from the grid. 
         */
        @Override
        public boolean onSingleTapUp(MotionEvent e) 
        {
            //my code
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) 
        {
            //my code
            super.onLongPress(e);
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                               float distanceX, float distanceY) 
        {
            //my code
            return true;
        }

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

        @Override
        public boolean onDown(MotionEvent e) 
        {
            //my code
            return true;
        }
}

My question is different from duplicate because I want to know why the Android GestureDetector assumes onFling instead onSingleTapUp. Or if I'm doing something wrong.

Jorge B.
  • 1,144
  • 2
  • 17
  • 37
  • 1
    Possible duplicate of [Fling gesture detection on grid layout](http://stackoverflow.com/questions/937313/fling-gesture-detection-on-grid-layout) – Shabbir Dhangot May 24 '16 at 14:14

1 Answers1

0

I solved the problem like this:

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

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

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

    if( posIni == posFin ) { 
         //onClick code
    }
    else {
         //onFling code.
    }
    return true;    
}
Jorge B.
  • 1,144
  • 2
  • 17
  • 37