Event distribution mechanism of View in Android -- Android development art exploration notes
Introduction
Event distribution of click events refers to the distribution process of MotionEvent events. When a MotionEvent is generated, the system needs to pass the event to a specific View, the transfer process is the distribution process.
The three methods involved are dispatchTouchEvent, which is used to distribute events. If an event can be passed to the current View, this method will be called, the returned result is affected by the onTouchEvent method of the current View and the dispatchTouchEvent method of the View, indicating whether the current event onInterceptTouchEvent is consumed: used to determine whether to intercept an event. If the current View intercepts an event, in the same event sequence, this method will not be called again. The returned result indicates whether to intercept the current event. onTouchEvent: called in the dispatchTouchEvent method to process click events, the returned result indicates whether the current event is consumed. If the event is not consumed, the current View cannot receive the event again in the same event sequence. Relationship between the three methods
public boolean dispatchTouchEvent(MotionEvent ev) { boolean consume = false; if(onInterceptTouchEvent(ev)) { consume = onTouchEvent(ev); } else { consume = child.dispatchTouchEvent(ev); } return consume; }
The above Pseudo Code describes the relationship between the three. If the current View intercepts an event, it is handed over to its onTouchEvent for processing. Otherwise, it is passed to the subview until the event is finally processed.
Event distribution order
When a click event is generated, the transfer process is as follows: Activity-> Window-> View. If the onTouchEvent of View returns false, its parent container onTouchEvent will be called, and so on, and will be processed by the onTouchEvent of Activity.
Activity event distribution process
Activity-> Window-> DecorView.
Windows is an abstract class that can be controlledTop ViewPhoneWindow is the only implementation of this class.
DecorView is the underlying container of the current interface, that is, the View set by setContentView is a subview of it.
Top-level View distribution of click events
ViewGroup-> dispatchTouchEvent-> onInterceptTouchEvent-> onTouch or onTouchEvent
A top-level View is generally a ViewGroup. After an event is intercepted, if mOnTouchListener is set in ViewGroup, The onTouch method in Listener will block onTouchEvent. If mOnClickListener is set for onTouchEvent, The onClick in Listener is called. If the ViewGroup is not blocked, it is passed to the subview until the entire event is distributed.
View processing of click events
If mOnTouchListener is set for the View, the onTouch method in the Listener will block the onTouchEvent. If mOnClickListener is set for onTouchEvent, The onClick in Listener is called.
View does not have the onInterceptTouchEvent method. Once a click event is passed to it, it will handle it.
Note: The above only describes the principles of the event distribution process. For source code analysis, refer to the relevant chapter in this document.