Events: When the finger touches the screen, the system will make a series of responses, which will be encapsulated into a series of MotionEvent objects. The event distribution mechanism is basically the transmission mechanism of these MotionEvent objects in the UI interface.

Common events: ACTION_DOWN (finger down), ACTION_MOVE (finger sliding on the screen), ACTION_UP (finger moving away from the screen), ACTION_CANCEL (a series of events, precursor events are processed by the view, but subsequent events are blocked by the parent view);

The event passing mechanism is basic: the event is first passed to the Activity and then distributed downward: Activity – >Phonewindow – >DecorView – >ViewGroup – >View; The process for handling the event is the opposite of the distribution process: View – >ViewGroup – >DecorView – >PhoneWindow – >Activity. Overall, the event distribution mechanism involves three main bodies: View, ViewGroup and Activity

The event distribution mechanism involves the method DispatchTouchevent: The core of the event distribution mechanism, which is responsible for all event scheduling. The return value is Boolean, normally we do not process, the default is passed down. All three bodies have this method; Activities and viewgroups use it to distribute events down, and views use it to distribute onClick, onLongClick, onTouch, and onTouchEvents;

OnInterceptTouchEvent: Only the ViewGroup has this method, indicating whether or not to intercept. If intercepted, the event is not passed to its child views and its onTouchEvent method is called instead. Activities and Views don’t have this method. The return value is Boolean, true means interception, false does not. The former is the start of the event, if intercepted, then the entire page no response, unreasonable. The latter belongs to the last event distribution, there is no need to intercept, just call onTouchEvent to determine whether to process the event.

OnTouchEvent: This method is available to all three principals and returns a Boolean value. If true, the event is consumed; if false, it is not consumed.

The event transmission mechanism is detailed: After the event is generated, the Activity’s dispatchTouchEvent is called to send down. Call the dispatTouchchEvent method of the ViewGroup, and onInterceptTouchEvent. If this method returns false, the event will be passed down to the View. If this method returns true, the event will be intercepted. If onTouchEvent returns false, the event is handled by the activity’s onTouchEvent. If onTouchEvent returns true, the event is handled by the activity’s onTouchEvent. The event is consumed, the viewGroup’s DispatchTouchEvent returns true, and the activity’s dispatchTouchEvent returns true. If the ViewGroup does not intercept the event, the view’s dispatchTouchEvent method is called, and then the View’s onTouchEvent method is called. If this method returns true, it consumes the event. The View dispatchTouchEvent returns true, the ViewGroup dispatchTouchEvent returns true, and the Activity dispatchTouchEvent returns true. If the View’s onTouchEvent returns false, indicating that the event is not consumed, the event is passed up to the ViewGroup, similar to analysis. If the ViewGroup also does not consume the event, the event is eventually thrown to the activity.

Onclick contains two events, ACTIONdown and ACTIONup, which must be at the end of the sequence because it is impossible to wait. Onlonhclick contains ACTIONdown but has a time requirement and is executed penultimate; Ontouch is user-defined and has a higher priority than onTouchEvent, the system default; Ontouch – > OnTouchEvent – > onLongClick – > onClick

View overlap response event: When clicking on two view overlap areas, the upper and lower layers are distinguished. 1. If the lower layer is clickable but the upper layer is unclickable, the lower layer responds to the event. 2. If the upper layer is clickable but the lower layer is unclickable, the upper layer responds to the event. 3. If both are clickable, the upper layer responds to the event, consumes the event, and the lower layer does not receive the event.

1. The onTouchEvent method returns true to consume the event, 2. The View will consume the event if it is clickable (Android: onClickable =true), regardless of whether the View itself registers the click event.

A series of events should all be understood by the same view consumption: to prevent event response clutter. ViewGroup uses a member variable mFirstTouchTarget to hold the child View information of the consumption event. Since Android supports multiple finger operations, this mFirstTouchTarget is a linked list of TouchTargets. The dispatchTouchEvent of the View can be divided into three stages: determine whether interception is needed; Find the child View that consumes the event and update it to mFirstTouchTarget; Events are redistributed based on whether they are intercepted and mFirstTouchTarget.