This blog post is the first of a series of blog posts on the Android touch event distribution mechanism, leading you to a global mastery of the Android touch event distribution mechanism. In particular, the source code interpretation is based on the latest Android6.0 version.
(a) Android6.0 source interpretation of view Click event Distribution mechanism
(ii) Android6.0 source interpretation of ViewGroup Click event Distribution mechanism
(iii) Android6.0 source interpretation of activity Click event Distribution mechanism
Why to interpret the touch event distribution mechanism
1. Mastering the View event distribution mechanism
2. Provide theoretical support for resolving view sliding conflicts
3. Learn about the latest Android code changes
Touch Touches Event
Touch touching events in Android include Click (OnClick), long-Press (Onlongclick), drag-and-drop (Ondrag), swipe (onscroll), and so on, click and double click, and also include single-finger and multi-finger operation. The first state of touch is action_down, which means that touch will have subsequent events, such as moving, lifting, and so on, a action_down, a action_up, a lot of action_move, Makes up many of the events in Android. All operation events must first be performed by pressing (Actiondown), after which all actions are taken as a precondition, when the pressing operation is complete, then a move (actionmove) is then lifted (ACTION_UP), Or you can simply lift the button without moving when the operation is done.
Press (Action_down)
Mobile (Action_move)
Lift Up (ACTION_UP)
Cancel gesture (Action_cancel)
Slide-out screen (action_outside)
Three important methods involved in the touch event distribution mechanism
publicbooleandispatchTouchEvent(MotionEvent ev)
Dispatchtouchevent is used for the distribution of events. If the event can be passed to the current view, then this method must be called, and the result will be affected by the Dispatchtouchevent method of the ontouchevent and downlevel view of the current view, indicating whether the current event is consumed.
publiconInterceptTouchEventevent)
Onintercepttouchevent is a method that ViewGroup provides to determine whether to intercept an event, and if the current view intercepts an event, the method is not called again in the same sequence of events, and the result indicates whether to intercept the current event. The default returns FALSE, which returns true for interception.
publiconTouchEventevent)
Ontouchevent is called in the Dispatchtouchevent method to handle the Click event, and the return result indicates whether the current event is consumed, and if not consumed, the current view cannot accept the event again in the same sequence of events. The default returns true in view, which indicates consumption of this event.
The distribution of these three important methods
The following two methods are included in the view:
public boolean dispatchtouchevent (Motionevent ev);
public boolean ontouchevent (Motionevent ev);
The following three methods are included in the ViewGroup:
public boolean dispatchtouchevent (Motionevent ev);
public boolean onintercepttouchevent (Motionevent ev);
public boolean ontouchevent (Motionevent ev);
The following two methods are included in the activity:
public boolean dispatchtouchevent (Motionevent ev);
public boolean ontouchevent (Motionevent ev);
Android Touch Event Distribution flowchart
Android Touch Event Flow Summary
1. An event sequence refers to a series of events that occur from the touch of a finger to the screen, to the end of the finger leaving the screen. The same sequence of events starts with the Action_down event and contains an indeterminate number of move events, ending with the Action_up event.
2. The order in which the events are passed is: Activity->window->view, that is, the event is always passed to the Activity first, then passed to the Window, and finally passed to view, after the top-level view receives the event, Events are distributed according to the event distribution mechanism.
3. The process of passing an event is internal, that is, the event is always distributed to child elements by the parent element.
4. Normally, an event sequence can be intercepted and consumed by only one view. Once an element intercepts an event, all events in the same sequence of events are handed over to it, so events in the same event sequence cannot be handled by two views at the same time, but can be done by special means, A view, for example, forcibly passes an event that is supposed to be handled by ontouchevent to other view processing.
5. Once a view has started processing an event, if it does not consume the Action_down event, then other things in the same sequence of events will not be handed over to it, and the event is handed back to its parent container for processing (calling the parent container's Ontouchevent method) If it consumes the Action_down event, but does not consume other types of events, the Click event disappears, the Ontouchevent method of the parent container is not called, and the current view can still receive subsequent events, but these events are eventually passed to activity processing.
6.Android Click event Distribution is when you reach the top view (typically ViewGroup), you call ViewGroup's Dispatchtouchevent method, Where its Onintercepttouchevent method returns True, the event pass is intercepted, the event is handled by ViewGroup, and if the Onintercepttouchevent method returns false, the event is not intercepted. False is returned by default. Then the Dispatchtouchevent method in the child view is called, and the event is passed to the next layer of view by the top view, and the next procedure is a recursive loop, consistent with the top-level view event distribution process, until the entire event is distributed.
Source interpretation of event distribution mechanism in view, viewgroup and activity
Children's shoes can refer to the following links for system learning
(a) Android6.0 source interpretation of view Click event Distribution mechanism
(ii) Android6.0 source interpretation of ViewGroup Click event Distribution mechanism
(iii) Android6.0 source interpretation of activity Click event Distribution mechanism
Interpretation of Android6.0 touch event distribution mechanism