Talking about the mechanism of Android event transmission

Source: Internet
Author: User

Presumably you will sometimes encounter the need to customize the control bar, this time will encounter the transmission and processing of events, very cumbersome Ah! Baidu above a ticket on the transmission mechanism of the event, but all the same, are dispatchtouchevent

Returns True, the event is intercepted, false is distributed to the child control, and so on, after several days of research found that this event is not said to be so simple and not imagined difficult.

General Click events, sliding events are made up of action_up, Action_moe, Action_down , Android apps by default, touch events are transferred from the activity, Assuming that the view does not need to intercept the event, then this touch event will walk around the inside of the view and back to the activity, if we are bound to a view of an event listener, then this event can only be consumed by a view.

In fact, we generally know the above, but when it is necessary to rewrite a control when it will be puzzled, because the light know that the above is not enough, this time we need to look at the source of research, through my tinker, found the law of event transmission:

Activity Delivery Order:dispatchtouchevent event pass to Window→window passed to the inside of the view→view Dispatchtouchevent

ViewGroup Order of delivery:dispatchtouchevent →onintercepttouchevent→ event is passed to the next layer →ontouchevent

dispatchtouchevent return to Ture, then this event will not go back, because the next layer was intercepted, The Ontouchevent method is only last called in the dispatchtouchevent, because the event is passed to the next layer, if the next layer is not executed to call ontouchevent this step, and onintercepttouchevent only ViewGroup, is the event of another interception. < Span style= "font-size:12px" >

This is the system default, but if you rewrite the dispatchtouchevent method above, you can subvert the default rule, such as ViewGroup passed to its child view, I return false in their related event interception method, and then to each of them to deal with their own events, then it is not an event can only be consumed by a view, temporarily on the water to this, it is only a bit of their own experience, Welcome to learn from each other Exchange

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Talking about the mechanism of Android event transmission

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.