Android: GestureDetector won't catch Gestures
Asked Answered
W

2

2

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?

Welterweight answered 26/10, 2010 at 20:8 Comment(0)
W
4

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.

Welterweight answered 26/10, 2010 at 22:20 Comment(3)
There 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.Dispirited
@Welterweight i am using it like this.......but its not working ....please have a look...... pastebin.com/n9JPGrCnAdminicle
It does not work on Dialog. Have you tested on Dialog?Billiards
S
1

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.

Swart answered 1/12, 2010 at 8:10 Comment(1)
or just return myGestureDetector.onTouchEvent(event)Soul

© 2022 - 2024 — McMap. All rights reserved.