One of the Android event mechanisms: event delivery and consumption

Source: Internet
Author: User

Http://www.cnblogs.com/lwbqqyumidi/p/3500997.html

About the event mechanism in Android, there are a lot of places to use, and this knowledge point is really a bit complicated.

Before writing this article, online read a lot of blog post, and some write the feeling is very good. It was just that it felt like understanding and it was easy to forget afterwards. Now you have to tidy up the system.

Android events in the form of many, such as Ontach, OnClick and Onlongclick, in the specific microscopic performance of the situation has Action_down, Action_move and action_up.

Regardless of the type of event performance, the first is the event-based delivery model. In fact, the event delivery in Android is somewhat similar to the event delivery model in JS. are based on the form of first capturing and then bubbling.

During the capture phase, the event is first received by an external view and then passed to the view of its inner layer, which is passed to the smallest view cell that is more sufficient to receive the event, completing the event capture process;

During the bubbling phase, the event starts with the smallest view cell of the event source, bubbles out in turn, and passes the event to the layer.

The capture and bubbling of events is the delivery process for the entire event, but in the actual delivery process, Android is relatively complex.

It is primarily manifested in the ability to control whether each layer of events continues to pass (coordinated by event distribution and event interception), and the specific consumption of events (which is carried out by the incident response, but it is important to note that event distribution itself has the capacity to consume events).

This is the event distribution, interception, and response mentioned in this article.

Different controls in Android have a slightly different event distribution, interception, and response, mainly manifested in the activity itself without event interception, not ViewGroup's smallest view unit without event distribution and event interception (because it does not have its own child view).

The specific correspondence is as follows:

For the control of the event delivery and consumption mechanism in Android, the most important thing to note is the return value of these methods.

Event Distribution: Public Boolean dispatchtouchevent (motionevent ev)

When a supervisor hears an event, it is first captured by the activity and enters the event distribution processing process. Whether it's activity or view, as mentioned above, event distribution itself also has the ability to consume,

If event distribution returns TRUE, the Change event is no longer distributed at this level and is already consumed in the event distribution itself. At this point, the incident has ended. If you don't want any of the controls in the activity to have any of the event spending power,

The simplest way to override this activity's Dispatchtouchevent method is to return true to OK.

If the event distribution returns false, the event is no longer being distributed at this level and is being consumed by the Ontouchevent method of the upper control.

Of course, if this layer of control is already activity, then the event will be consumed or processed by the system.

If the event distribution returns the system's default super.dispatchtouchevent (EV), the event is distributed to this layer's event interception onintercepttouchevent method for processing

(If the layer control is activity, because it has no event interception, the event will be passed directly to the child view and handed to the child view's event distribution for processing).

Event Interception: Public Boolean onintercepttouchevent (motionevent ev)  

If Onintercepttouchevent returns True, the event is intercepted and the intercepted event is referred to the ontouchevent of the layer control;

If the returned result is false, the event is not intercepted and the event is successfully distributed to the child view. and processed by the dispatchtouchevent of the child view.

If the super.onintercepttouchevent (EV) is returned, the event is not intercepted by default and is processed by the dispatchtouchevent of the child view.

Event Response: Public boolean ontouchevent (motionevent ev)

If Ontouchevent returns True, the event is consumed after the ontouchevent has finished processing the event. At the end of the event, no subsequent bubbling will take place.

If Ontouchevent returns false, the event continues to bubble to the upper view and is processed by the ontouchevent of the upper view after processing in ontouchevent.

If Super.ontouchevent (EV) is returned, the default processing logic is the same as the return false.

Summary: From the above process can be seen, whether the dispatchtouchevent return True or FALSE, the event is no longer distributed,

Only when it returns to Super.dispatchtouchevent (EV) does it indicate a desire to distribute to the lower layer,

However, if it is possible to distribute the success, it needs to be audited by the event intercept onintercepttouchevent. Whether the event has bubbling is determined by the return value of the ontouchevent.

Test Case:

One of the Android event mechanisms: Event delivery and consumption

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.