Android Event Distribution parsing

Source: Internet
Author: User

Event distribution mechanism
One. Click event Propagation Path
1. Click event TouchEvent first to reach activity, and then to the activity corresponding to the window, and then to Decorview, and then to the ID content of the ViewGroup, That is, we set the ViewGroup through Setcontentview to the last view. What we can control by programming is activity,viewgroup and view.

2. To pass a click event to a view or viewgroup, to call its dispatchtouchevent (), to determine if the view or ViewGroup is intercepted, to invoke its onintercepttouchevent (), To determine whether the view or viewgroup is consuming, call its ontouchevent (). The child view does not have a onintercepttouchevent (), so the click event passed to the sub view is directly judged if it is consumed. When an event is consumed, it disappears.


3. The custom ViewGroup and view default is not to intercept the click event, ViewGroup decide whether to intercept or not is determined by the return value of Onintercepttouchevent () ViewGroup determines whether the consumption of events is determined by ontouchevent (). View because there is no onintercepttouchevent (), all the decision to intercept or not and consumption is determined by the return value of Ontouchevent (), true means interception, false means not to intercept. Attention to distinguish between interception and consumption.
ViewGroup's Onintercepttouchevent () is as follows



4. There is a piece of code inside the Dispatchtouchevent () of the child view

The logic is that if view is set to Ontouchlistener, its Ontouchlistener Ontouch () method is called. If the method returns False, Ontouchevent () is called, but if the method returns True, the Ontouchevent () method is not called, and this is the case that the view consumes the Click event. And our usual setonclicklistener () set of Onclicklistener is executed in ontouchevent (), and if we do not rewrite ontouchevent (), Ontouchevent () returns True.





Two. Specific situation analysis
1. When ViewGroup and view do not intercept the Action_down when the click event is not consumed, the process is as follows

and later Action_move and ACTION_UP will not be transmitted to viewgroup and view, but directly to the activity of the ontouchevent () processing
2, if the ViewGroup does not intercept, in the view also does not consume, then when the event again came to ViewGroup ontouchevent (), if ViewGroup decided to consume the Action_down, Then the Action_move and action_up of the touchevent will be passed to the ViewGroup and will not be called onintercepttouchevent ()

3. When ViewGroup's onintercepttouchevent () intercepts the Action_down, the click event is no longer passed to the child view, which calls ViewGroup's ontouchevent () to determine if it consumes action_ Down, if not consumed, the activity's ontouchevent () is called, and later the Action_move and action_up of the TouchEvent series are no longer passed to ViewGroup, which is the child view below.

If consumed, then the Action_move and action_up of the TouchEvent series will be passed on to the ViewGroup (when it is not passed to its child view), And no longer calls the ViewGroup's Onintercepttouchevent ().



4. For Action_down, when ViewGroup does not intercept, the child view decides to consume. Then the Action_move and ACTION_UP after the event will normally pass through ViewGroup's dispatchtouchevent (), then Onintercepttouchevent (), Then to the sub-view of the ontouchevent (), even if not by the view and viewgroup consumption, the same event system Action_move and ACTION_UP will normally pass in ViewGroup and view. Note that when the child view does not consume the event, the event is not passed to ViewGroup's ontouchevent (), but directly to the activity

5. When Action_down to ViewGroup, if the viewgroup does not intercept, the event is passed to the child view, if the child view is consumed, but when the first action_move to ViewGroup, ViewGroup intercept, then, This action_move will pass to the child view, triggering the ontouchevent of the child view, but then Action_move becomes action_cancel, if the child view consumes, then the action_cancel disappears, If the child view is not consumed, the action_cancel is passed to the activity's ontouchevent (). However, no matter the sub-view consumption does not consume Action_cancel, the second beginning of the Action_move and ACTION_UP will not be transmitted to the child view, the second is to get ViewGroup dispatchtouchevent (), And then directly to the ViewGroup ontouchevent () ViewGroup decided to consume the consumption, not consumption back to the activity.

6. When ViewGroup does not intercept action_down and action_move, the child view is intercepted. Then ViewGroup intercept action_up, at this time, action_up will be action_cancel form to the child view, sub-view processing Action_cancel The event disappears, not processing is passed back to the activity.

Three. Android built-in view analysis
The custom view default is not to consume click events. And Android comes with the view is not necessarily, to the view of the clickable and longclickable at the same time is not consumed by the Click event, the main one is not false, you want to consume the click event. TextView default is not to consume a click event unless you set the Ontouchlistener or Onclicklistener for it. The button is consumed by the Click event, unless it is set to not clickable. (Strictly speaking, the event is judged as the onclick must be action_down,action_move,ation_up all occur within the size range of the view).


Four. The activity branch process method
1. Internal interception method:
Ideas: ViewGroup First do not intercept click events, first to the child view, sub-view first processing, when the conditions should be intercepted by the ViewGroup, the incident to viewgroup processing.
Implementation: ViewGroup in Onintercepttouchevent () do not intercept action_down, to the child view. ViewGroup is called setdisallowintercepttouchevent (true) when Action_down is encountered in the Dispatchtouchevent () of the child view. That is, the Action_move and action_up of the TouchEvent series are set without ViewGroup. Then this time the child view in Ontouchevent () must consume Action_down, otherwise the event even ViewGroup will not be transmitted. Well, now the Action_down will pass through ViewGroup's dispatchtouchevent () and then directly to the Dispatchtouchevent () of the child view, And will not be handed over to ViewGroup's onintercepttouchevent (), this time the child view can be consumed according to the situation Action_move, when the sub-view think that after the Click event to the parent container, That is, the Setdisallowintercepttouchevent (false) of the parent container is called. And when rewriting viewgroup, the events outside of Action_down are set to intercept, After the use of the Click event after the ViewGroup dispatchtouchevent () will reach its onintercepttouchevent (), after interception to ViewGroup ontouchevent () processing.
The logical diagram is as follows:

Code implementation:
ViewGroup

Child view




2. External interception
Implementation: ViewGroup to Action_down do not intercept, and sub-view to consume Action_down (otherwise this series of click events will not be transmitted to the ViewGroup and view). Then for Action_move, if ViewGroup does not intercept, it will pass to the child view, if ViewGroup want to intercept, then the event will be passed to ViewGroup ontouchevent () processing. And, ViewGroup don't have to intercept action_up.

Code:






3. Slide treatment Method Three:
The first thing to understand is that the return value of onintercepttouchevent () is generally the return value of Dispatchtouchevent (). Therefore, the return value from Dispatchtouchevent () can be directly intercepted or not. and Dispatchtouchevent () is called at every event flow, unlike Onintercepttouchevent (), Onintercepttouchevent () when ViewGroup decides to intercept and consumes the event in Ontouchevent, the same sequence of events is no longer going through it.
In this case, you can make a more flexible approach to the process, but this method is more troublesome than the above two.
Idea: To do interception processing in ViewGroup and to invoke the Dispatchtouchevent () pass event of the child view.
Judge Action_down,action_move,action_up in Dispatchtouchevent () respectively.
When the Action_down arrives, return true to indicate that the next event will be passed over. Then call the child view of the Dispatchtouchevent (), the Action_down event to the child view, sub-view to have normal logic.
When the Action_move arrives, if ViewGroup wants to intercept, do not invoke the Dispatchtouchevent () of the child view, so that the event stays in the ViewGroup and not into the child view. If you do not want to intercept a action_down,viewgroup and want to give the child view processing, then call the child view's dispatchtouchevent () directly. When Action_up arrives, its dispatchtouchevent () is called if the child view needs to execute normally.
Be careful not to call Super.dispatchtouchevent (), because the interception logic here is made by ourselves, so we can even not rewrite ViewGroup's onintercepttouchevent () and Ontouchevent ().


Android Event Distribution parsing

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.