Android:GestureDetector 无法捕获手势

发布于 2024-09-29 09:06:07 字数 2184 浏览 4 评论 0原文

我的程序中必须有 GestureDetectors。一个工作得很好,另一个则不然。据我所知,它们都是以相同的方式实现的。

下面是实现不起作用的代码:

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;
    }
});

然后我稍后有这个块,它定义了leaveGestureDetector

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;
     }
}

无论出于何种原因,它根本不注册fling。无论动画是否发生,程序都应该打印出我试图跟踪的 Log.d("MotionEvent","onFling") 但它没有。我所知道的是,它确实注册了某种触摸事件的发生,因为它确实从我显示的第一个块中追踪出了“Excuse Gesture Return”“false”。你有什么想法为什么它不会记录这次猛击吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尴尬癌患者 2024-10-06 09:06:07

我不太确定为什么,但是一旦我将所有可能的手势的覆盖放在 SimpleOnGestureListener 中,它就开始工作。显然它需要所有这些,而不仅仅是 onFling。

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.

天生の放荡 2024-10-06 09:06:07

这不是你所需要的,你可能也做了什么,让它为你工作的是你的活动重写 onTouchEvent somtehin,如下所示:

@Override
public boolean onTouchEvent(MotionEvent event) {
  if (myGestureDetector.onTouchEvent(event)) {
    return true;
  } else {
    return false;
  }
}

我在 这个优秀的网站

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文