What causes a MotionEvent.ACTION_CANCEL in Android?

后端 未结 4 1759
情书的邮戳
情书的邮戳 2021-02-03 19:25

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 onTouchListener contains a <

相关标签:
4条回答
  • 2021-02-03 20:18

    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."

    Hope that is the answer you are looking for:

    Resources: Motion Event, Stack Overflow.

    0 讨论(0)
  • 2021-02-03 20:18

    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

    0 讨论(0)
  • 2021-02-03 20:20

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

    0 讨论(0)
  • 2021-02-03 20:21

    ACTION_CANCEL is triggered by ancestor to notify all descendants that they lost onTouch control and it's will be responsible for handling the next onTouch event. Usually it is caused when a descendant returned true in onTouch or onTouchEvent method but after that, during the next of touch event of gesture, an ancestor returned true in onInterceptTouchEvent()

    [Touch event flow]

    0 讨论(0)
提交回复
热议问题