Android event distributed delivery process

Android event is triggered from outside to inside, the return is from the inside out, probably introduced today at an event distribution mechanism
First, look for the process, Android event distribution is activity-> DecorView-> view group-> view of the process , mainly related methods are

 @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

Event distribution, this distribution method is mainly played the role of

 @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev);
    }

This method is to intercept event delivery, mainly in the ViewGroup will have to return to true representative of events is not allowed to pass on to the sub-View, will trigger the current View of onTouchEvent (), for consumption events; return false events are not representative of intercept, the event can be passed to the child view, default return false

 @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return super.onTouchEvent(ev);
    }

This method is a touch event, as a gesture set of parameters, this is the monitor user's gestures, then the callback method touchlistener of onTouch. If we just click the button with your finger, and then immediately release, onTouch event will only execute ACTION_DOWN and ACTION_UP action; if using a mobile phone click of a button, and also slide your finger on the button for a moment, then slide in the process, ACTION_MOVE action on It will not stop execution

So how onClick event is triggered it, how to perform only onTouch without executing onClick it?
From the source point of view, it is the first implementation of onTouch, according to the user's gestures and return value to decide whether to execute the onClick event, that onTouch performed before onClick, and can affect the implementation of the onClick.
If you want to perform onTouch event and does not execute down the distribution, then, simply onTouch method's return value to true, it will execute only onTouch event is not executed onClick event

Summarizes the event delivery mechanism for calling sequence:
ViewGroup event delivery method:
dispatchTouchEvent
onInterceptTouchEvent
onTouchEvent
: View event delivery method
dispatchTouchEvent
onTouchEvent
only ViewGroup container have onInterceptTouchEvent method. Because as the last child view no need to intercept, it has been unable to continue to pass down the event, whether the intercept has no meaning.

Reproduced in: https: //www.jianshu.com/p/8bd6b82beec8

Guess you like

Origin blog.csdn.net/weixin_34119545/article/details/91095976