Android event distribution mechanism (detailed)

Source: Internet
Author: User

In Android development, the event distribution mechanism is an important knowledge system of Android, understanding and familiar with the entire distribution mechanism to better analyze the various click-slip failures, better to extend the control of the event function and develop custom controls, At the same time the event distribution mechanism is also one of the Android interview must be asked, if you can put some of the following event distribution map on the spot to draw a lot of certainly. Needless to say, a summary: The event distribution mechanism is very important.

Android Event Distribution Stream

About the Android event distribution mechanism on-line blog many, but many are to write a demo and then paste the output of the log or source analysis, and then a bunch of comments and instructions, if the intentions to see is certainly a lot of harvest but it is difficult to clear the whole process and remember. Once also desperately want to remember the whole process, but a period of time and forget, finally feel the analysis of this problem and the trend of the flow of events, a diagram to explain and explanation will be clear a lot, below I according to the picture of an event distribution flowchart, the event from the user after the click, in different functions return value of the final trend.

Figure 1.
Note:

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" word 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 kind of U-shape diagram, We look at this picture, it may be better to understand the meaning of the U-chart.


Figure 2.
So if we don't override the method inside the control or change the return value, and call the parent class directly with super, the entire event flow should be from Activity-->viewgroup->view Call the Dispatchtouchevent method from the top down until the leaf node (View) is called, and the Ontouchevent method is called from the view->viewgroup->activity.

2,dispatchtouchevent and Ontouchevent once return true, the event stops passing (to the end) (no one can receive the event again). Fancy as long as the return true event will not continue to pass, 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 again.


Figure 3.

3,dispatchtouchevent, and Ontouchevent return False when the event is passed back to the parent control's ontouchevent processing.


Figure 4.

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. **


Paste_image.png

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


Figure 5.
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.


Figure 6
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, and then viewgroup how the Dispatchtouchevent method can distribute the event to its own ontouchevent processing, return True and false are not, then only through the interceptor to intercept the event to their own ontouchevent, so ViewGroup The super default implementation of the Dispatchtouchevent method is to call onintercepttouchevent and remember this.
So what happens to the Dispatchtouchevent return Super.dispatchtouchevent () of the view, and unfortunately the view has no interceptors. 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 an 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 an interceptor. To allow 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 are dispatchtouchevent for event distribution, so this event may be distributed in four destinations

Note:--> What is needed to represent the event target.
1, their own consumption, the end of transmission. ——->return true;
2, to their own ontouchevent processing ——-> Call super.dispatchtouchevent () system default will go to call Onintercepttouchevent, in Onintercepttouchevent Return true to divide the event into its own ontouchevent processing.
3, passed to the 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 incident to the sub-View-->return false,viewgroup default is not intercepted, so super==false;

About Action_move and action_up

The above is for the Action_down event delivery, Action_move and action_up in the process of transmission is not the same as Action_down, you do action_down when the return of false, A series of other actions will no longer be executed. Simply put, it is when Dispatchtouchevent is doing event distribution that only the previous event (such as Action_down) returns true to receive Action_move and ACTION_UP events. Specifically, many bloggers have said this, but what is the specific meaning? Let's take a look at the specific analysis below.

As mentioned above, if the event is not interrupted, it will continue to go down to the leaf layer (View), and then continue to upload to activity,dispatchtouchevent and ontouchevent can pass the return true consumption events, the end of event delivery, And onintercepttouchevent can not consume events, it is equivalent to a fork in the diversion of the role of diversion, Action_move and action_up will be in which functions are called, before said that not which function received the Action_down, You will receive a follow-up event such as Action_move.
Below, we look at the specific trends of action_move events and ACTION_UP events under different scenarios and summarize the rules.

1. * * Our dispatchtouchevent method in ViewGroup1 returns true consumption of this event

Action_down event is passed from (activity's dispatchtouchevent)--–> (ViewGroup1 dispatchtouchevent) After the event is consumed (such as a red Arrow code Action_ The direction of the down event). **

//打印日志Activity|dispatchTouchEvent--ACTION_DOWNViewGroup1|dispatchTouchEvent--ACTION_DOWN---->消费


In this scenario Action_move and ACTION_UP will do, look at the following out of the log

Activity | dispatchtouchevent -->Action_move ViewGroup1 | dispatchtouchevent -->Action_move----toucheventactivity | dispatchtouchevent -->action_up ViewGroup1 | dispatchtouchevent -->action_up

In
The Red Arrows represent the direction of the Action_down event.
The blue arrows represent the direction of the Action_move and ACTION_UP events.

2, We in ViewGroup2 dispatchtouchevent return true consumption of this event

Activity | dispatchtouchevent -->Action_down ViewGroup1 | dispatchtouchevent -->Action_downViewGroup1 | onintercepttouchevent -->Action_downViewGroup2 | dispatchtouchevent -->Action_down---->ConsumerActivity | dispatchtouchevent -->Action_move ViewGroup1 | dispatchtouchevent -->Action_moveViewGroup1 | onintercepttouchevent -->Action_moveViewGroup2 | dispatchtouchevent -->Action_move----toucheventactivity | dispatchtouchevent -->action_up ViewGroup1 | dispatchtouchevent -->action_upViewGroup1 | onintercepttouchevent -->action_upViewGroup2 | dispatchtouchevent -->action_up----

The Red Arrows represent the direction of the Action_down event.
The blue arrows represent the direction of the Action_move and ACTION_UP events.


Paste_image.png
3. * * We return true in view dispatchtouchevent consumption of this event
This I do not draw, the effect and in ViewGroup2 dispatchtouchevent return true, similar to the same received Action_down Dispatchtouchevent function can receive Action_ Move and Action_up. **
So we can basically conclude that if the dispatchtouchevent of a control returns a true consumption end event, then the function receiving Action_down can receive Action_move and ACTION_UP.

4. We return true in view Ontouchevent to consume this event
The Red Arrows represent the direction of the Action_down event.
The blue arrows represent the direction of the Action_move and ACTION_UP events.

5, We in ViewGroup 2 of Ontouchevent return True consumption this event
The Red Arrows represent the direction of the Action_down event.
The blue arrows represent the direction of the Action_move and ACTION_UP events.

6, We in ViewGroup 1 of Ontouchevent return true consumption this event
The Red Arrows represent the direction of the Action_down event.
The blue arrows represent the direction of the Action_move and ACTION_UP events.

7. we return true in ontouchevent of activity to consume this event
The Red Arrows represent the direction of the Action_down event.
The blue arrows represent the direction of the Action_move and ACTION_UP events.

8. we return false in the view's dispatchtouchevent and the activity's ontouchevent return true to consume this event
The Red Arrows represent the direction of the Action_down event.
The blue arrows represent the direction of the Action_move and ACTION_UP events.

9, we return false in the view dispatchtouchevent and ViewGroup 1 of the ontouchevent return true consumption this event
The Red Arrows represent the direction of the Action_down event.
The blue arrows represent the direction of the Action_move and ACTION_UP events.

10. we return false in the view's dispatchtouchevent and return True at ViewGroup 2 ontouchevent to consume this event
The Red Arrows represent the direction of the Action_down event.
The blue arrows represent the direction of the Action_move and ACTION_UP events.

11. we return false in the dispatchtouchevent of ViewGroup2 and return true in ViewGroup1 ontouchevent to consume this event
The Red Arrows represent the direction of the Action_down event.
The blue arrows represent the direction of the Action_move and ACTION_UP events.

12, we onintercepttouchevent in ViewGroup2 return true to intercept this event and in ViewGroup 1 ontouchevent return True consumption of this event.
The Red Arrows represent the direction of the Action_down event.
The blue arrows represent the direction of the Action_move and ACTION_UP events.

Drawing a lot of pictures at once, there are several cases will no longer draw, I believe you also see the law, for the situation in Ontouchevent consumption event: in which view ontouchevent return True, then Action_move and Action_ The up event is no longer passed down from the top down to the view, and it passes directly to its ontouchevent and ends the event delivery process.

For Action_move, action_up Summary: Action_down event in which control consumption (return True), then Action_move and Action_ Up will be sent from the top down (through the dispatchtouchevent) to do the event distribution down, will only upload to this control, will not continue to preach, if the Action_down event is dispatchtouchevent consumption, then the event stops delivery, If the Action_down event is consumed in Ontouchevent, the Action_move or Action_up event is passed to the control's ontouchevent processing and the pass is ended.

Original link: http://www.jianshu.com/p/e99b5e8bd67b#

Android event distribution mechanism (detailed)

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.