The previous period of time just contact with the development of Android mobile phone, its event propagation mechanism is not very understanding, although the Internet also looked up the relevant information, but always feel vaguely understood, specious, so I wrote a small demo test a bit. Finally figured out its specific mechanism. Write down your conclusions, share them, and hope to help people who are beginners to Android
Layout effect:
Figure 1
Refer to the first to say the concrete results:
1) Onintercepttouchevent is responsible for blocking the touch event, and the first thing to do with nested view is the Onintercepttouchevent method of the outermost view. Then execute the onintercepttouchevent of the child view in turn, and then execute the event interception method in the child view's child view (assuming, of course, that all onintercepttouchevent of the nested view will be executed, Onintercepttouchevent return FALSE for each view). Reference, so the onintercepttouchevent execution order is a--->b--->c--->d. That is, the parent view is passed to the child view. In summary, the event interception mechanism is initiated by the parent view to intercept the event (something happened to me first, son later). Referring to when a finger touches an event, parent view a first initiates an interception of the event, and if a intercept fails, it is intercepted by its sub-view B, and if B interception fails, it is passed to sub-view C of B and then intercepted. Until a child view has successfully intercepted the event.
2) The success or failure of a view interception event is identified as the return value of the Onintercepttouchevent method, when it returns true to indicate that the interception was successful, and that the current view failed to intercept the event when it returns false.
3) The following is a case of interception success, assuming that the C view intercepts the current touch event successfully. The interception success means that this event will no longer be passed to D view. So at this point the D view of the onintercepttouchevent is not running (the event can not be reached, but also to intercept who?) )。 After the event interception succeeds, the event is then processed and the method of processing is taught to the Ontouchevent method. When the C view intercepts successfully, then the Ontouchevent method of C view is executed, does that mean that the current touch event is handled by the C view's Ontouchevent method? This is determined by the return value of the Ontouchevent method of the C view. When the ontouchevent of the C view returns true, the current event is handled by C in its sole discretion, dealing with the various actions of course, what Motionevent.action_move,action_ The up is handed to the C Ontouchevent method for processing. So at this point in the C ontouchevent method of the switch (event.getaction) to determine the execution of the relevant logic. If the return is false, the C view does not handle this event or can not handle it, what to do? No, my son. Dad came, so the incident was in the Ontouchevent method of the B view. The same B to deal with this event or not to see the ontouchevent return value of B, the specific explanation is the same as C, no more words.
4) in the case where both onintercepttouchevent and ontouchevent of a B C D are returned false, the order in which the methods are executed is a.onintercepttouchevent--> B.onintercepttouchevent-->c.onintercepttouchevent-->d.touchevent (Deepest sub-view not rewritten onintercepttouchevent)-- C.touchevent-->b.touchevent-->a.touchevent. That is, the interception event is that the parent view takes precedence over the child view to intercept, and the processing event is the child view precedence parent view for processing.
Summary: Onintercepttouchevent is responsible for intercepting events, and then handing them over to the view that first encountered Ontouchevent return True.
An explanation of the Android event interception processing mechanism.--------Turn