3

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.

ThunderSS
  • 443
  • 1
  • 7
  • 17

3 Answers3

2

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;
}
e.perrot
  • 139
  • 5
1

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.

Manfred Moser
  • 29,539
  • 13
  • 92
  • 123
  • Thanks for answering so fast. I added the new class, that extends GridView, and changed all of the GridViews in my activity to GestureGridView (my new class), however now I get the ClassCastException: android.widget.Gridview. I'm confuesed, should I not have changed all the GridViews to GestureGridViews? Nothing happens then. I'd appreciate every help. – ThunderSS Mar 02 '12 at 12:50
  • I think I found the solution of the error given above - I simply didn't change the .xml file. However, after I did it, I have two new errors - first one is android.view.InflateException: Binary XML file line #44: Error inflating class pl.flamis.Kalendarz$GestureGridView and the second one: java.lang.NoSuchMethodException: GestureGridView(Context,AttributeSet). Any ideas? – ThunderSS Mar 02 '12 at 14:30
  • Yeah, I solved that problem, but now I have a new one. I had a declaration - public static GridView calendar; now I changed it to public static GestureGridView calendar; I get an error in the lines: calendar = (GestureGridView)findViewById(R.id.calendar); cell_adapter = new CellAdapter(this); calendar.setAdapter(cell_adapter); because the calendar is null - I don't know why; Shall I post the source code of the whole class here (or send you by mail or somehow else)? I have no idea, what to do. – ThunderSS Mar 02 '12 at 17:58
  • dont use static and just debug it and see if it finds it the view.. maybe your changed the id in the xml – Manfred Moser Mar 02 '12 at 18:52
  • when I delete the "static" modifier, I get errors like "Cannot make static reference to non-static fields". id in xml file is correct. – ThunderSS Mar 02 '12 at 19:23
  • I would have to see more code.. but it on github or pastebin or wherever – Manfred Moser Mar 02 '12 at 19:48
  • it seems like it doesn't find the view properly, don't know why. When I used the class constructor firs - debugger showed me, that calendar was properly initialized, but after findViewById it was null again. Shall I find the custom views differently? – ThunderSS Mar 02 '12 at 19:50
  • Manfred, here's practically the whole package - http://pastebin.com/u/ThunderSS. I skipped some irrelevant classes. My goal was to make fling gesture work on GridView, therefore I created the new class, like you suggested. I really appreciate you helping me on this. – ThunderSS Mar 02 '12 at 20:01
  • I would have to step through.. but it looks like you are using findViewById in way too many places.. use it once and store the found view in a variable..also now sure about invalidating and then finding it again.. – Manfred Moser Mar 02 '12 at 20:28
  • Well, I'll correct that and see what happens. However, thank you very much for your help. – ThunderSS Mar 02 '12 at 20:33
  • Sorry.. but I cant do it for you. Not enough time. I know this approach works for scrollview and fling (I got it working here..) – Manfred Moser Mar 02 '12 at 22:59
1

I actually found another solution, because the way I tried to do it simply didn't work and nobody knows why. I used GestureOverlayView instead, and it works great. Just had to add two swipe gestures to the gesture library.

ThunderSS
  • 443
  • 1
  • 7
  • 17