GestureDetector 上的 OnUp 事件

发布于 2024-11-02 05:54:10 字数 149 浏览 3 评论 0原文

我的问题很简单,实现 GestureListener 时 onUp 事件在哪里?

我在手势检测器上有很多事件,不能只使用侦听器的 onUp 事件,因为其中一个事件是需要它的 onSingleTapConfirmed

My question is simple, where's the onUp event when implementing a GestureListener?

I has a lot of events on the gesturedetector, and cannot just consume the onUp event of the listener, cause one of events are the onSingleTapConfirmed that needs it.

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

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

发布评论

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

评论(4

锦欢 2024-11-09 05:54:10

尝试使用onFling()。看起来像是一个奇怪的名字,但对我来说,它的工作原理就像 onUp() 方法在逻辑上一样有效。或者试试这个方法...

@Override
public boolean onTouchEvent(MotionEvent me) {

  Log.v("ADAM", "ontouch: " + me.getAction());

  if(me.getAction() == 0){
    Log.v("ADAM", "Down");
  }
  else if (me.getAction() == 1) {
    Log.v("ADAM", "Up");
  }
  else if (me.getAction() == 2) {
    Log.v("ADAM", "Scroll");
  }
  boolean detectedUp = event.getAction() == MotionEvent.ACTION_UP;
  if (!mGestureDetector.onTouchEvent(event) && detectedUp) {
    return onUp(event);
  }
  return true;
}

Try using onFling(). Seems like a weird name but for me, it works as the onUp() method would logically work. Or try this method...

@Override
public boolean onTouchEvent(MotionEvent me) {

  Log.v("ADAM", "ontouch: " + me.getAction());

  if(me.getAction() == 0){
    Log.v("ADAM", "Down");
  }
  else if (me.getAction() == 1) {
    Log.v("ADAM", "Up");
  }
  else if (me.getAction() == 2) {
    Log.v("ADAM", "Scroll");
  }
  boolean detectedUp = event.getAction() == MotionEvent.ACTION_UP;
  if (!mGestureDetector.onTouchEvent(event) && detectedUp) {
    return onUp(event);
  }
  return true;
}
凉城凉梦凉人心 2024-11-09 05:54:10

我通过在将 Action 事件传递给 GestureListener 之前读取它来修复它

img.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
      if (arg1.getAction() == MotionEvent.ACTION_UP
          && !getDocument().isScaled()) 
        img.notifyUp();
      return gestures.onTouchEvent(arg1);
    }
});

I fixed it by reading the Action event before I pass it to the GestureListener

img.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
      if (arg1.getAction() == MotionEvent.ACTION_UP
          && !getDocument().isScaled()) 
        img.notifyUp();
      return gestures.onTouchEvent(arg1);
    }
});
会傲 2024-11-09 05:54:10

其他答案可以解决问题,也许它们效果很好,但我不认为这些是好的解决方案。
因为我们应该在单个模块中处理事件,这样我们可以更方便地维护或扩展代码。
(顺便说一句,不要使用 onFilng() ,如果你的手指在屏幕上缓慢移动,onFilng() 将不起作用)
所以,我的解决方案是重写 GestureDetector

public class ModifyGestureDetector extends GestureDetector {

    MyGestureListener myGestureListener;

    public ModifyGestureDetector(Context context, OnGestureListener listener) {
        super(context, listener);
        init(listener);
    }

    void init(OnGestureListener listener){
        if (listener instanceof MyGestureListener){
            myGestureListener = (MyGestureListener) listener;
        }
    }

    //u can write something more complex as long as u need
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_UP 
            && myGestureListener != null){
            myGestureListener.onUp(ev);
        }
        return super.onTouchEvent(ev);
    }

    public interface MyGestureListener{
        public void onUp(MotionEvent ev);
    }
}  

以及监听器

public class MyGestureListener extends GestureDetector.SimpleOnGestureListener
        implements ModifyGestureDetector.MyGestureListener {


    @Override
    public void onUp(MotionEvent ev) {
        //do what u want
    }
} 

::)

The other answers can solve the question,maybe they work good,but I don't think those are good solutions.
Because we should deal with the event in a single moudle,so that we can be esaier to maintain or expand the code.
(By the way, don not use onFilng() ,if your finger move slowly on the screen,onFilng() wouldn't work)
So,my solution is rewriting the GestureDetector:

public class ModifyGestureDetector extends GestureDetector {

    MyGestureListener myGestureListener;

    public ModifyGestureDetector(Context context, OnGestureListener listener) {
        super(context, listener);
        init(listener);
    }

    void init(OnGestureListener listener){
        if (listener instanceof MyGestureListener){
            myGestureListener = (MyGestureListener) listener;
        }
    }

    //u can write something more complex as long as u need
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_UP 
            && myGestureListener != null){
            myGestureListener.onUp(ev);
        }
        return super.onTouchEvent(ev);
    }

    public interface MyGestureListener{
        public void onUp(MotionEvent ev);
    }
}  

And the listener:

public class MyGestureListener extends GestureDetector.SimpleOnGestureListener
        implements ModifyGestureDetector.MyGestureListener {


    @Override
    public void onUp(MotionEvent ev) {
        //do what u want
    }
} 

: )

Oo萌小芽oO 2024-11-09 05:54:10

您是否从 GestureDetector.OnGestureListener 中消耗的其他事件返回了 true?我遇到了这个问题,结果如果你返回 false,系统会假设你不感兴趣并停止向你传递事件,所以你错过了!

Have you returned true from the other events you consume in your GestureDetector.OnGestureListener? I had this problem and it turns out if you return false, the system assumes you aren't interested and stops passing you events, so you miss the up!

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