Android Touch-screen event deliveryA: Preface before due to do launcher part, involves a lot of interactive parts, because the project in the past a long time, and recently in doing gts,cts test and modification, many are not familiar with or even forget, combined with other people's article experience (I found that some places and others discrepancies, But self-test verification), coupled with their own tests, today write an article to summarize.
two: About ViewGroup with view views ViewGroup is a container class, in general, the layout container is inherited word it. Containers can be stored in the view, of course, can also nest containers, such as relativelayout,linearlayout and so on. What I'm talking about here is that the view view is a class that inherits only the view and does not inherit the container class (ViewGroup). Because the small difference in the passing of the event to the back is related to this. classes that inherit from ViewGroup have intercepttouchevent (), and classes that do not inherit viewgroup do not have this method, which is responsible for intercepting the touch event. Three: Event delivery1: In general, for ViewGroup, there are three methods for event delivery.
- dispatchtouchevent (); Responsible for event distribution, if the return value is true, indicates that the event does not continue to be distributed and is not consumed (execution ontouchevent (), if Ontouchevent () Returns true to indicate that consumption is complete, false indicates that no consumption is completed, and then the Dispatchtouchevent () of the container class is executed one level up, and the return value of each level is true, if the Dispatchtouchevent () return value is False, To Intercepttouchevent () to the parent class;
- Intercepttouchevent (); Responsible for event interception, if return false, indicates no interception, touch event to the next level of view processing, if intercepted, then to this level of Ontouchevent () to deal with;
- ontouchevent (); Responsible for consumption events, if the return value is true, indicates consumption of the event, the event disappears, and if the return value is False, Ontouchevent () is executed up to a level, and if the previous level continues to return false, Then continue upward, if each level is returned false, then into the dead loop, causing Anr,android 5.1 will also die cycle, I test will not cause the ANR (probably the company's phone is not well-equipped), in order to avoid this situation, can be in the activity of the ontouchevent () Returns true;
... extends activity{public boolean dispatchtouchevent (motionevent ev) { return true; }}
2: For view that is not a container class
- dispatchtouchevent (); Responsible for event distribution, if the return value is true, indicates that the event does not continue to be distributed and is not consumed (execution ontouchevent (), if Ontouchevent () Returns TRUE indicates that consumption is complete, false indicates that no consumption has been consumed), note here that the view is generally present in a container, but does not execute the parent container's dispatchtouchevent (), but instead executes the parent container's dispatchtouchevent (), and then recursively pass up , why? Personally think is: because there is no inheritance viewgroup (not the container class), it does not have a child view, he and the parent container's dispatchtouchevent () processing is the same as the call, so although there is an event distribution function If the dispatchtouchevent () return value is False, the parent class is assigned to ontouchevent () consumption ; personally feel it is best not to forcibly return false inside Dispatchtouchevent () and do UI processing inside.
- ontouchevent (); Responsible for consumption events, if the return value is true, indicates consumption of the event, the event disappears, and if the return value is False, Ontouchevent () is executed up to a level, and if the previous level continues to return false, Then continue upward, if each level is returned false, then into the dead loop, causing Anr,android 5.1 will also die cycle, I test will not cause the ANR (probably the company's phone is not well-equipped), in order to avoid this situation, can be in the activity of the ontouchevent () Returns true;
3: dispatchtouchevent () and ontouchevent () on activity;an event starts with the dispatchtouchevent () method of the activity and passes to Decorview (the view inside the Setcontentview ()).
stay on the code
Android Touchscreen event Delivery (Personal practice summary)