Reputation: 443
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.
Upvotes: 3
Views: 3116
Reputation: 443
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.
Upvotes: 1
Reputation: 139
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;
}
Upvotes: 2
Reputation: 29912
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.
Upvotes: 1