---restore content starts---
First, what is
The first thing we need to know about event distribution is that when a touch event occurs, from a window to a view, to a view, to the process of being consumed.
Second, what to do
In the process of learning Android, we often do not know why to learn these theories, they are boring, difficult to understand, and even when we are in the initial stage of development in the complete use of these theories.
So we have to learn some technology and know what it can do.
Relatively simple, when doing development, how to deal with the problem of sliding conflict.
Third, the analysis of the event distribution mechanism.
First here, this section is not too much analysis of the distribution of the source code, the next section of analysis, interested can go to see Guo Lin blog, the source analysis is very clear, here I mainly explain some of my own understanding of it.
Let's get to know three ways first.
Dispatchtouchevent (motionevent EV)//For the distribution of events
Onintercepttouchevent (motionevent EV)//called in Dispatchtouchevent () to determine whether to intercept an event
Ontouchevent (motionevent EV)//called in dispatchtouchevent () to handle click events
Pseudo-code Analysis for easy comprehension
public boolean dispatchtouchevent (Motionevent ev) {
Boolean consume = false;
if (onintercepttouchevent (EV)) {
consume = ontouchevent (EV);
} else{
Consum = child.dispatchtouchevent (EV);
}
return consum;
}
First, we will divide the event distribution mechanism according to two main characters, one is ViewGroup, one is view.
For ViewGroup
When a click event is generated, the Dispatchtouchevent () method is called. If the Onintercepttouchevent () method returns True to indicate that the event was intercepted, return false then the event will be handed to the child view to continue with the above steps until the event is processed.
For view
Handles the event, sets Ontoucheventlistener (), OnTouch () callback, returns False,ontouchevent () is called, and if Setonclicklistener () is set at this time, the onclick () method is called. Return True,ontouchevent () will not be called, when Setonclicklistener () is set, the OnClick () method is not called. This is the distribution mechanism of the view
In short, the above two parts constitute the event distribution mechanism, because there is no analysis of the source code, so it looks very simple, draw a flowchart is like this
After studying this section, presumably on the Android event distribution have a general understanding, but there may be doubts, such as the OnClick method is sometimes set up and will not be called, and so on, I will analyze the source code next.
Add: 1, click the event after the occurrence, follow the following order Activity->window->view
2, when all view does not handle the event (Ontouchevent () returns false), the event is returned to the activity processing
3, Ontouch and ontouchevent, are called in Dispatchtouchevent (),Ontouch takes precedence over ontouchevent execution. If the event is consumed by returning true in the Ontouch method, Ontouchevent will no longer execute. It is also important to note that Ontouch can be executed with two prerequisites, the value of the first Montouchlistener cannot be null, and the second currently clicked control must be enable. So if you have a control that is non-enable, then registering it with the Ontouch event will never be executed. For this type of control, if we want to listen to its touch event, it must be implemented by overriding the Ontouchevent method in the control.
On the mechanism of Android event distribution (I.)