【问题标题】:How do I get my view to stop listening to touch events after ACTION_MOVE?如何让我的视图在 ACTION_MOVE 之后停止监听触摸事件?
【发布时间】:2014-01-31 04:02:58
【问题描述】:

我有一个按钮,我正在按下按钮进行动画处理。我希望它在拖到某个阈值之外后恢复到“正常”状态。

我在ACTION_DOWN 上创建了一个视图边界矩形,并检查它是否超出ACTION_MOVE 的触摸区域。我成功检测到“越界”触摸,但我无法让视图停止监听触摸。就像它忽略了我的 animateToNormal() 方法。

我尝试将布尔返回值更改为 true 而不是 false,但没有帮助。我还尝试在ACTION_MOVE 案例中删除触摸侦听器(设置为空),但我需要重新附加以继续侦听触摸。我想我可以在添加回来之前添加任意延迟,但这似乎是一个可怕的黑客攻击。

我正在 4.2 设备 (LG G2) 上对此进行测试。

private static class AnimationOnTouchListener implements View.OnTouchListener {
        private Rect rect;

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            switch(motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    rect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
                    animatePressed();
                    return false;

                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    // back to normal state
                    animateBackToNormal();
                    return false;

                case MotionEvent.ACTION_MOVE:
                    if(!rect.contains(view.getLeft() + (int) motionEvent.getX(), view.getTop() + (int) motionEvent.getY())){
                        d(TAG, "out of bounds");
                        animateBackToNormal();
                        // STOP LISTENING TO MY TOUCH EVENTS!

                    } else {
                        d(TAG, "in bounds");
                    }
                    return false;
                default:
                    return true;
            }
        }

【问题讨论】:

标签: android android-animation ontouchlistener


【解决方案1】:

为什么你只是不继续听,而是设置一个语句来忽略运动事件?

类似的东西:

private static class AnimationOnTouchListener implements View.OnTouchListener {
        private Rect rect;
        private boolean ignore = false;

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if(ignore && motionEvent.getAction()!=MotionEvent.ACTION_UP)
           return false;
        switch(motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                rect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
                animatePressed();
                return false;

            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                // back to normal state
                animateBackToNormal();

                // IMPORTANT - touch down won't work if this isn't there.
                ignore = false;
                return false;

            case MotionEvent.ACTION_MOVE:
                if(!rect.contains(view.getLeft() + (int) motionEvent.getX(), view.getTop() + (int) motionEvent.getY())){
                    d(TAG, "out of bounds");
                    animateBackToNormal();
                    // STOP LISTENING TO MY TOUCH EVENTS!
                    ignore = true;
                } else {
                    d(TAG, "in bounds");
                }
                return false;
            default:
                return true;
        }
    }

【讨论】:

  • 我误解了返回值的作用。谢谢!我确实必须对您的代码进行一次调整(在ACTION_UP 中添加ignore = false;,但总体上可行!
  • 顺便提一下,在我看来这似乎是一个快速的解决方案,但不是最好的,如果您找到“更多”正确的解决方案,请在此线程中发布
  • 你说得很好。我将不再接受您的回答,以鼓励其他人发帖。如果没有其他答案,我会接受。
猜你喜欢
  • 2016-09-22
  • 1970-01-01
  • 2023-03-14
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-07
  • 1970-01-01
相关资源
最近更新 更多