Android event distribution mechanism source code analysis

Source: Internet
Author: User

Little emotion, do Android for some time, has always been used to organize notes stored in Youdao notes, no blog habits.

After the gradual sorting out, also count "review" again-_-.

There are three methods related to the event distribution of Android:

1. Publicbooleandispatchtouchevent (motionevent ev)

2. Publicboolean onintercepttouchevent (motionevent ev)

3. Publicbooleanontouchevent (motionevent event)

The first method indicates whether the event is distributed, and the second method indicates whether to intercept the event (only ViewGroup has this method.) View is not), the third method indicates whether to consume events.

Before analyzing the source code, let's summarize the pattern of event distribution, or the usage of the above 3 methods:

① When touchevent occurs, the activity first passes touchevent to the topmost view, usually a viewgroup.

The TouchEvent first arrives at the topmost view of the dispatchtouchevent and is then distributed by the Dispatchtouchevent method. Returns true if not distributed. All events are handed over to dispatchtouchevent for processing. Suppose Dispatchtouchevent returns false. The view and its child view are not receiving a possibly event, assuming that the call to Super.dispatchtouchevent is given to intercepttouchevent processing.

② assumes that Intercepttouchevent returns True, which is the interception. The event may be given to its ontouchevent to deal with intercepttouchevent no longer processing (assuming that the hand is picked up in another click, the down event will walk once, and the back move and up are not gone). Assuming Ontouchevent is not handled, the event is returned on the original path. Perhaps the event will not be given to this view, assuming intercepttouchevent return false or call Super.intercepttouchevent, then the event is still intercepttouchevent processing, But not through ontouchevent.

Ordinary view without Intercepttouchevent method does not consider this method, other laws are equally

③ for Ontouvhevent returns true for consumption events, false for no consumption, Call super.ontouchevent time of the two cases, for viewgroup and so can put sub-view to not consume events, for can not put sub-view of the view to consume events. When the event arrives at the bottom of the view when the incident is not consumed, it will return, just walk ontouchevent, may be consumed by the upper view

Suppose you are concerned about the use of these methods. Then you define the view yourself. There should be no God horse problem to see here. Anyway, I know how these methods will affect the distribution of events. As for why I don't care.

But as an active learner of high-quality programmers, we not only need to make clear how to use. Also make clear why these situations occur (there should be applause here). We will follow the above three point-by-point analysis.

First we look at the 1th: dispatchtouchevent.

This method returns false to indicate that the event is not distributed, so it is understood that neither the view nor the child view will consume events. That maybe the incident will not be to you, anyway gave you do not spend it. Why give it to you, this is very good understanding, the code implementation is to save all the consumption of the view of the event is saved, so no consumption of the event view is not subject to a possible event, this part of the code is not posted, See line No. 2213 of the ViewGroup code to call the code of the Addtouchtarget method. According to the conventional thinking. Now that the return false means no consuming event, then it should be a consumption event to return true? No no no,too young and simple.

Suppose you write a demo to try to find the dispatchtouchevent method has been gone, but the event is not distributed, sub-view can not receive events, only the return value is super.dispatchtouchevent talent to distribute the event ...

Nani, what the hell is this? Do not follow the routine of the card. Well, it's just a case of flipping through the source code. We analyze the source code for android6.0 (API level 23).

The following code is an excerpt from the ViewGroup Dispatchtouchevent method. In line 2167, I take the sub view. Then to 2197 rows, here called a method, the above is taken to the sub-view as a reference to pass the past.

The following code is mentioned in the dispatchtouchevent called in this method, see line No. 2553, when the child is not empty, call the child's Dispatchtouchevent(detailed will go to 2553 or 2575 rows. They are essentially the same, the difference is to pass over the motionevent carried out a split operation, detailed what did not go deep into.

There are small partners who know their differences can leave a message).

Is there an enlightened feeling here? ViewGroup is able to distribute events to child view because of the event distribution method that calls child view in dispatchtouchevent, assuming that you are in ViewGroup's dispatchtouchevent The method returns only true without returning super. dispatchtouchevent, then the method of the child view's event distribution will not be called. The child view will not get the event. Clear, I think I said is quite clear. In the middle we ignore other irrelevant code. Suppose you want to know more, can read the source code, read the blog reading source code, all so easy~.

The following method is used later. This method was called multiple times in the dispatchtouchevent method.

Look at the 2nd Onintercepttouchevent method: This method indicates whether to intercept the event. Assume that it returns true. Then the event will be dealt directly to its own ontouchevent. Why is that? Look at the following code block:

Line No. 2104, press the phone screen, go here, 2106 lines, here disallowintercept the default case here is False (default initialized value calculation), will go to 2108 rows, call Onintercepttouchevent, Suppose we replicate this method to return true, this is the value of intercept is true, and then go down to 2238 rows. The mfirsttouchtarget is null at this time. Will walk to 2240. Here again the Dispatchtransformedtouchevent method is called, which is the second block of code in this article. At this point the third parameter, child, is null, and the method goes to 2547 or 2566 rows (in detail which one.) Whatever). The Dispatchtouchevent method of the parent class is then called. Let's look at the parent class method:

See the Code in the red box No, directly called the ontouchevent. So assuming that your onintercepttouchevent returns TRUE, it calls its own ontouchevent, and the event is passed to its ontouchevent.

3rd, why the event will be passed back to the parent view when it is not consumed, I have a little word poor.

。。

Do not know how to describe the narrative, the reason is recursion. The parent view passes the event recursively when the dispatchtouchevent is called. When the event does not have quilt view consumption. Will call its own Ontouchevent method, so the effect from the log looks like the event is returned.

About their own understanding of this aspect. So much on the whole. The source code of the resolution is not too specific, it is probably a preliminary arrangement, may understand the existence of problems or even wrong, welcome to correct.

Android event distribution mechanism source code analysis

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.