Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - GridView - GestureDetector doesn't work

I have a problem with detecting fling gestures in my app. My layout consists of a GridView, couple of TextViews and buttons.

I implemented OnGestureListener:

public class MyActivity extends Activity implements OnGestureListener{
private GestureDetector myGesture ;

then in OnCreate:

myGesture = new GestureDetector(this);

and Overridden methods:

@Override
public boolean onTouchEvent(MotionEvent event){
    return myGesture.onTouchEvent(event);
}

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // TODO Auto-generated method stub
    try {
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
            return false;
        if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            //right to left fling
        }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            //left to right fling
        }
    } catch (Exception e) {
        // nothing
    }
    return false;
}

And this actually works great, but NOT on the GridView. Wherever outside the GridView I perform the fling, it works. On the GridView - there's absolutely no reaction. I have literally no idea, what to do about it, so thanks for any help in advance.

like image 447
ThunderSS Avatar asked Jul 10 '26 01:07

ThunderSS


2 Answers

Do you return true in your onFling when the event is consumed (like below)?

    @Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // TODO Auto-generated method stub
    try {
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
            return false;
        if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            //right to left fling
        }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            //left to right fling
        }
        return true;
    } catch (Exception e) {
        // nothing
    }
    return false;
}
like image 188
e.perrot Avatar answered Jul 11 '26 18:07

e.perrot


Afaik a gridview automatically also adds ScrollView features and they intercept the gesture detection. You will have to implement your own GridView that overrides that behaviour and adds in the fling detection. There are a number of examples for ScrollView on stackoverflow and other sites. Just do the similar approach for GridView.

like image 29
Manfred Moser Avatar answered Jul 11 '26 18:07

Manfred Moser