Original link http://sparkyuan.me/reprint please indicate the origin of the introduction
The event distribution of a Click event is the distribution process for the Motionevent event. When a motionevent is produced, the system needs to pass the event to a detailed view, and the process of passing it is the process of distributing it.
The three methods involved
- Dispatchtouchevent: Used to distribute events, assuming events can be passed to the current view. Then this method is bound to be called, and the result is affected by the ontouchevent of the current view and the view's Dispatchtouchevent method, indicating if the current event is consumed
- Onintercepttouchevent: Used to infer whether or not to intercept an event, assuming that the current view intercepts an event, then in the same event sequence where the method is not called again, and the result indicates whether to intercept the current event;
- Ontouchevent: Called in the Dispatchtouchevent method. Used to handle a click event, the return result indicates whether the current event is consumed, assuming that it is not consumed, in the same sequence of events. The current view cannot receive the event again.
Three ways to relate to each other
publicbooleandispatchTouchEvent(MotionEvent ev) { booleanfalse; if(onInterceptTouchEvent(ev)) { consume = onTouchEvent(ev); else { consume = child.dispatchTouchEvent(ev); } return consume; }
The above pseudo-code is a very good description of the relationship between the three. Assuming that the current view interception event, it will be handed to their own ontouchevent to deal with, or passed to the child view. Until the event is finally processed.
Event Distribution Order
When a click event has been generated. Its delivery steps are as follows: Activity--Window--View. Assuming that the view's Ontouchevent returns false, its parent container ontouchevent will be called. And so on Will eventually be handled by the ontouchevent of the activity.
Activity-to-event distribution process
Activity, Window, Decorview.
Windows is an abstract class. You can control the appearance and behavior policies of top-level view , and Phonewindow 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 child view of it.
The distribution process for top-level view-to-click events
ViewGroup, Dispatchtouchevent, onintercepttouchevent, OnTouch or ontouchevent
Top view is generally a viewgroup.
After intercepting the event, assuming that ViewGroup set the Montouchlistener, the Ontouch method in listener will block out ontouchevent. Suppose Ontouchevent set the Monclicklistener. The onclick in the listener will be called. Assuming that ViewGroup is not intercepted, it passes to the child view until the entire event is distributed.
View-to-click event handling Process
Assuming that the view is set to Montouchlistener, the Ontouch method in listener will block ontouchevent out.
Assuming that ontouchevent sets the Monclicklistener, the onclick in the listener is called.
View does not have a onintercepttouchevent method, once a click event is passed to him. He'll take care of it.
Note: Above is just a description of the event distribution process principle, about the source code analysis please refer to the corresponding chapters of the book.
Welcome reprint. Reprint please specify the source http://blog.csdn.net/l664675249/article/details/50738102
Event distribution mechanism for view in Android--android developing art exploration notes