Android: GestureDetector won't catch Gestures
Solution 1
I'm not really sure why, but as soon as I put Overrides for ALL of the possible gestures in a SimpleOnGestureListener it started working. Apparantly it needed them all in there, not just onFling.
Solution 2
This is not needed what you probably also did and what makes it work for you is that your activity override the onTouchEvent somtehing like this:
@Override
public boolean onTouchEvent(MotionEvent event) {
if (myGestureDetector.onTouchEvent(event)) {
return true;
} else {
return false;
}
}
I found the answer on this excellent site.
![LoneWolfPR](https://i.stack.imgur.com/KONfs.jpg?s=256&g=1)
LoneWolfPR
I'm a professional developer. On a day to day basis I use HTML, JavaScript, JQuery, CSS, XML, PHP and MySQL. On a less regular basis I develop for Android and iOS.
Updated on June 13, 2022Comments
-
LoneWolfPR about 2 years
I have to GestureDetectors in my program. One works beautifully, the other doesn't. As far as I can tell they're both implemented the same way.
Here's the code for implementing the one that isn't working:
myExcuseGestureDetector = new GestureDetector(new excuseGestureDetector()); excuseView.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if(myExcuseGestureDetector.onTouchEvent(event)){ Log.d("Excuse Gesture Return","true"); return true; } Log.d("Excuse Gesture Return","false"); return false; } });
Then I have this block later which defines excuseGestureDetector
private class excuseGestureDetector extends SimpleOnGestureListener{ @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Log.d("MotionEvent","onFling"); try { if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false; // right to left swipe if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { if(currExcuseNumber<currExcuseSet.size()){ currExcuseNumber++; loadNextExcuse(currExcuseNumber,1); excuseView.setInAnimation(slideLeftExcuseIn); excuseView.setOutAnimation(slideLeftExcuseOut); excuseView.showNext(); return true; } } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { if(currExcuseNumber > 1){ loadNextExcuse(currExcuseNumber,0); excuseView.setInAnimation(slideRightExcuseIn); excuseView.setOutAnimation(slideRightExcuseOut); excuseView.showPrevious(); return true; } } } catch (Exception e) { // nothing } return false; } }
For whatever reason, it doesn't register the fling at all. Regardless of whether the animation happens or not, the program should print out the Log.d("MotionEvent","onFling") that I'm trying to trace and it doesn't. All I know is that it does register that a touchevent of some sort has occured because it does trace out "Excuse Gesture Return" "false" from the first block I showed. Any thoughts on why it won't register the fling?
-
botteaap over 13 yearsThere are two things here: your "onTouch()" handler in the view, which should return if it has consumed the touch event. The GestureDetector might need multiple events though. So you shouldn't just return myExcuseGestureDetector.onTouchEvent(event) like you do now, but you should feed every event into GestureDetector and return true to indicate that you consumed the touch. It's most likely working now because you're GestureDetector always returns true.
-
Animesh Mangla over 8 years@LoneWolfPR i am using it like this.......but its not working ....please have a look...... pastebin.com/n9JPGrCn
-
Jason Hu over 7 yearsor just
return myGestureDetector.onTouchEvent(event)
-
zeeali over 7 yearsIt does not work on
Dialog
. Have you tested onDialog
?