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, the second method indicates whether to intercept the event (only ViewGroup has this method, view is not), and the third method indicates whether the event is consumed.
Before analyzing the source code, let's summarize the pattern of event distribution, or how to use the above 3 methods:
① When touchevent occurs, the activity first passes touchevent to the topmost view, which is generally a viewgroup. TouchEvent first to the topmost view of the dispatchtouchevent, then distributed by the Dispatchtouchevent method, return True is not distributed, all events are given to dispatchtouchevent processing, If Dispatchtouchevent returns false, the view and its child view are not receiving subsequent events, and if Super.dispatchtouchevent is called, Be handed over to the intercepttouchevent for processing.
② If Intercepttouchevent returns True, that is, intercept, then the subsequent event is given to its ontouchevent to handle the intercepttouchevent no longer processing (if the hand is picked up in a re-click, the down event will walk again, The back move and up do not go), if the ontouchevent is not processed, the event is returned, the subsequent event is not given to this view, if Intercepttouchevent returns false or call Super.intercepttouchevent, then the subsequent events are still intercepttouchevent processed, but not ontouchevent. Ordinary view without Intercepttouchevent method does not consider this method, other laws are the same
③ 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 of the non-consumption event, for can not put sub-view of the view to consume events. When the event arrives at the bottom of the view when it is not consumed, it will return, leaving only ontouchevent, which may be consumed by the upper view.
If you are only concerned about the use of these methods, and then customize the view, then see here should be no God horse problem, anyway I know how these methods will affect the distribution of events, as for why I do not care
But as an active learner of high-quality procedural apes, we not only have to figure out how to use them, but also understand why these situations occur (applause should be 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, then it can be understood that the view and the child view will not consume events, then the subsequent events will not be given to you, anyway gave you do not spend it, why give you, this is very good understanding, code implementation is to all the consumption of the event view is saved, So the view of the non-consumption event is not subject to subsequent events, this part of the code is not posted, see the No. 2213 line of the ViewGroup code calls the code of the Addtouchtarget method. According to the conventional thinking, since the return of false means no consumption events, then the return to true should be a consumption event it? No no no,too young and simple. If you try to write a demo, you will find that the dispatchtouchevent method has been gone, but the event has not been distributed, the child view does not receive the event, only the return value is super.dispatchtouchevent to distribute the event ... Nani, what the hell is this, not by the routine of the card ah. Well, that's the only source of the situation. We analyze the source code of android6.0 (API level 23).
The following code is an excerpt from ViewGroup's Dispatchtouchevent method, where 2167 rows are taken to sub-view I. Then to 2197 rows, here called a method, the above to take the sub-view as one of the parameters passed.
The following code is mentioned in the Dispatchtouchevent call in this method, see the No. 2553 line, when the child is not empty, call the child's Dispatchtouchevent(specific 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, specifically did not go into the drill. There are small friends who know their differences can leave a message). Is there an enlightened feeling here? ViewGroup is able to distribute events to child view because the event distribution method of child view is called in Dispatchtouchevent, if you are in ViewGroup dispatchtouchevent The method returns only true without returning super. dispatchtouchevent, then the method of the child view's event distribution will not be called, and the child view will not get the event. See, I think what I said is quite clear. In the middle we ignore other unrelated code, if you want to know more, you can read the source, read the blog reading source, all so easy~.
The following method is also used later, the method is called in thedispatchtouchevent method several times.
Look at the 2nd Onintercepttouchevent method: This method indicates whether to intercept the event. If true, the event is given to its own ontouchevent processing. 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, If we make a copy of this method returns True, this is the value of intercept is true, then go down will go to 2238 rows, this time mfirsttouchtarget is null, will go to 2240. Here again called the Dispatchtransformedtouchevent method, which is the second block of code in this article, when the third parameter, the child is null, the method will go to 2547 or 2566 rows (specifically which one, whatever) , and then call the parent class's Dispatchtouchevent method, and we'll look at the parent class's method:
See the Code in the red box No, directly called the ontouchevent. So if your onintercepttouchevent returns TRUE, it will call its own ontouchevent, and the event will be passed to its ontouchevent.
3rd, why the event is not consumed will be passed back to the Father view, I have a little word poor ... Do not know how to describe, the reason is recursion. The parent view passes the event recursively calling Dispatchtouchevent, and when the event is not consumed by the quilt view, it calls its own Ontouchevent method, so the effect from the log appears to be that the event was sent back.
About their own understanding of this, on the whole so much, the source of the analysis is not too detailed, it is probably a preliminary arrangement, may understand the existence of problems or even wrong, welcome correction.
Android event distribution mechanism source code analysis