是什么导致了 Android 中的 MotionEvent.ACTION_CANCEL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11960861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:18:45  来源:igfitidea点击:

What causes a MotionEvent.ACTION_CANCEL in Android?

androiddebuggingmotionevent

提问by Phil

I am working through debugging some touch handling stuff on Android, and am trying to figure out why the MotionEvent sent to my View's onTouchListenercontains a cancelaction. I have not been able to find any documentation on its cause, and was hoping someone could point me in the right direction for debugging this problem - error codes, source code, or some general knowledge.

我正在调试 Android 上的一些触摸处理内容,并试图弄清楚为什么发送到我的视图的 MotionEventonTouchListener包含取消操作。我无法找到有关其原因的任何文档,并希望有人能指出我调试此问题的正确方向 - 错误代码、源代码或一些一般知识。

回答by 0gravity

Is this what you are looking for:

这是你想要的:

"ACTION_CANCEL occurs when the parent takes possession of the motion, for example when the user has dragged enough across a list view that it will start scrolling instead of letting you press the buttons inside of it. You can find out more about it at the viewgroup documentation: onInterceptTouchEvent."

“ACTION_CANCEL 发生在父级控制动作时,例如当用户在列表视图中拖动足够多的时候它会开始滚动而不是让你按下里面的按钮。你可以在视图组中找到更多关于它的信息文档:onInterceptTouchEvent。”

Hope that is the answer you are looking for:

希望这是您正在寻找的答案:

Resources: Motion Event, Stack Overflow.

资源:运动事件堆栈溢出

回答by Marc Alexander

All you need is to call

你只需要打电话

requestDisallowInterceptTouchEvent(true);

on the parent view, like this -

在父视图上,像这样 -

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            view.getParent().requestDisallowInterceptTouchEvent(true);
            switch(motionEvent.getActio){
            }

            return false; 

         }

Source: onInterceptTouchEvent, onTouchEvent only see ACTION_DOWN

来源:onInterceptTouchEvent,onTouchEvent 只看到 ACTION_DOWN

回答by yoAlex5

ACTION_CANCELis triggered by ancestor to notify all descendants that they lost onTouchcontrol and it's will be responsible for handling the next onTouchevent. Usually it is caused when a descendant returned true in onTouchor onTouchEventmethod but after that, during the next of touch event of gesture, an ancestor returned true in onInterceptTouchEvent()

ACTION_CANCEL由祖先触发通知所有后代他们失去了onTouch控制,它将负责处理下一个onTouch事件。通常是由后代在onTouchonTouchEvent方法中返回真引起的,但之后,在手势的下一个触摸事件中,祖先在或方法中返回真引起onInterceptTouchEvent()

[Touch event flow]

【触摸事件流程】

回答by Chitranshu Asthana

When the drag moves out of view rect, you get ACTION_CANCEL

当拖动移出视野矩形时,您将获得 ACTION_CANCEL