Android Event distribution mechanism diagram

Source: Internet
Author: User
Tags terminates

In the Android development of the event distribution is more important, it is more difficult to understand, before looking at this aspect of things, thinking that they understand, also did not pay much attention to the recent interview, think about this piece of things, looking back to find and forget, really good memory than bad writing Ah, Long time no use of this piece of things, also put behind the brain, today used a half day, see the information have a look, the piece of content to tidy up, the province later in the study process, to constantly warm so, look at the time will not be so troublesome.

Article referenced to: http://www.jianshu.com/p/e99b5e8bd67b

Here is the flowchart for event distribution:

    • Look carefully, the figure is divided into 3 layers, from the top down to the activity, ViewGroup, View
    • The event starts with the white arrow in the upper-left corner and is distributed by the dispatchtouchevent of the activity.
    • The upper word of the arrow represents the return value of the method (return True, return false, return super.xxxxx (), and Super means calling the parent class implementation.
    • Dispatchtouchevent and Ontouchevent have a "true----> Consumption " in the box, meaning that if the method returns True, then the event will not continue to pass to other places, the event terminates.
    • At present all the diagram events are for Action_down, and for Action_move and action_up we make the final analysis.
    • The activity in the previous diagram is dispatchtouchevent wrong (the figure has been fixed), only the return super.dispatchtouchevent (EV) is going down, the return TRUE or False event is consumed (terminating delivery).

Looking closely at the entire picture, we draw a few conclusions about the flow of events (hopefully the reader will concentrate on the 1, see it a few times, and have a clearer idea of the brain.) )
1. If the event is not interrupted, the entire event flow is a class U-chart, let's take a look at this diagram and probably understand the meaning of the U-chart.

So if we don't override or change the return value of the method inside the control, and call the parent class directly with super, the entire event flow should be from the activity---->viewgroup--->view Call the Dispatchtouchevent method from the top down until the leaf node (view), and then call the Ontouchevent method from the bottom up by the view--->viewgroup--->activity.
2, Dispatchtouchevent and ontouchevent once return true, the event stops passing (reaching the end) (no one can receive this event again)。 Fancy as long as the return true event will not continue to pass down, for return true we often say that the event is consumed, consumption means that the event came here is the end, will not go down, no one can receive this event again3, Dispatchtouchevent, and Ontouchevent return False when the event is passed back to the parent control's ontouchevent processing.

Look at the dark blue Line, for cases where false is returned, the event is passed to the parent control ontouchevent processing.

    • The meaning of the return false for dispatchtouchevent should be that the event stops at the same time as the child view delivery and distribution and begins to backtrack toward the parent control (the ontouchevent of the parent control starts from down to back until a ontouchevent return true), the event distribution mechanism is like recursion, and the meaning of return false is recursive stop and start backtracking.
    • For ontouchevent return false is simpler, it is not consuming events, and let the event continue in the direction of the parent control from bottom to top flow.

4, Dispatchtouchevent, Ontouchevent, onintercepttouchevent
The default implementation of these methods of ViewGroup and view is to let the entire event install U-shape complete, so return super.xxxxxx () will let the event follow the direction of the U-shape complete the entire event flow path), do not make any changes in the middle, do not backtrack, do not terminate, Every step of the way.

So if you see the method return Super.xxxxx () then the next direction of the event is to go to the U-shaped next target, slightly remembering the above image, you can quickly determine which of the next direction is which function of the control.


5, the role of onintercepttouchevent

Intercept the meaning of the interception, each viewgroup each time in the distribution, ask the Interceptor to intercept (that is, ask yourself if this event should not be handled by themselves) if you want to handle it, return in the Onintercepttouchevent method. True will be given to their own ontouchevent processing, if not intercept is to continue to the child control down. The default is not to intercept, because the child view also needs this event, so the Onintercepttouchevent interceptor return Super.onintercepttouchevent () and return false is the same, is not intercepted, the event will continue to pass to the dispatchtouchevent of the child view .

6, ViewGroup and view of the Dispatchtouchevent method return Super.dispatchtouchevent () when the event flow trend.




First look at the viewgroup of the Dispatchtouchevent, said before the return true is the end of delivery. Return false is the ontouchevent that goes back to the parent view, then viewgroup how the Dispatchtouchevent method can be used to distribute events to their own ontouchevent processing, return true and false are not, Then only through the interceptor to intercept the event to their own ontouchevent, so the ViewGroup dispatchtouchevent method of the Super default implementation is to call Onintercepttouchevent, Remember this
Then for ViewDispatchtouchevent return Super.dispatchtouchevent () where will the event go, and unfortunately the view does not have an interceptor. But the same truth is that return true is the end. Return false is the ontouchevent of the parent class of backtracking, how to distribute the event to its own ontouchevent processing, that can only return super.dispatchtouchevent, The default implementation of the Dispatchtouchevent () method of the view class is to help you invoke view's own Ontouchevent method.

Said so much, do not know to have to say clearly no, my side finally summed up:

    • For Dispatchtouchevent,ontouchevent,return True is the end event delivery. Return False is the Ontouchevent method that is traced back to the parent view.
    • ViewGroup want to distribute themselves to their ontouchevent, need the Interceptor Onintercepttouchevent method return true to intercept the event.
    • The ViewGroup Interceptor Onintercepttouchevent is not intercepted by default, so return Super.onintercepttouchevent () =return false;
    • View does not have interceptors, in order for view to distribute events to its own ontouchevent,view, the dispatchtouchevent default implementation (super) is to distribute the event to its own ontouchevent.

ViewGroup and view Dispatchtouchevent is doing event distribution, then this event may be distributed out of the four targets

Note:------> Back represents the event target needs to be done.
1, their own consumption, the end of transmission. ------->return true;
2, to their own ontouchevent processing-------> Call super.dispatchtouchevent () system by default will call Onintercepttouchevent, When Onintercepttouchevent return True, the event is assigned to its own ontouchevent processing.
3, pass to Child view------> Call Super.dispatchtouchevent () The default implementation will go to call Onintercepttouchevent in Onintercepttouchevent return false, The event is passed to the subclass.
4, does not pass to the child view, the event terminates down passes, the event begins to backtrack, from the parent view's ontouchevent starts the event from the bottom to the upper return executes each control's ontouchevent------->return false;
Note: because view does not have a child view, it does not require onintercepttouchevent to control whether the event is passed to child view or intercept, so the view event distribution calls Super.dispatchtouchevent () By default the event is passed to its own ontouchevent processing (equivalent to interception), compared to the ViewGroup dispatchtouchevent event distribution, view event distribution without the above mentioned 4 targets 3rd.

The Ontouchevent method for ViewGroup and view is for event handling, so this event can only be handled in two ways:

1, their own consumption, the end of the event, no longer passed to who----->return true;
2, continue to upload from the next, do not consume events, so that the parent view can also receive this event----->return false; The default implementation of view is not consumed. So super==false.

The Onintercepttouchevent method of ViewGroup has two conditions for events:

1, interception down, to their own ontouchevent processing--->return true;
2, do not intercept, the event down to the sub-view---->return False,viewgroup default is not interception, so super==false;

Android Event distribution mechanism diagram

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.