Event distribution mechanism for view in Android--android developing art exploration notes

Source: Internet
Author: User

Welcome reprint, Reprint please indicate the source http://blog.csdn.net/l664675249/article/details/50738102

Introduced

Click event distribution is the process of distributing the Motionevent event, and when a motionevent is produced, the system needs to pass the event to a specific view, which is the process of distribution.

The three methods involved
    • Dispatchtouchevent: Used for the distribution of events, if the event can be passed to the current view, then this method must be called, the return result is affected by the current view of the ontouchevent and view of the Dispatchtouchevent method That indicates whether the current event is being consumed
    • Onintercepttouchevent: Used to determine whether to intercept an event, if the current view intercepts an event, then in the same sequence of events, this method will not be called again, the results indicate whether to intercept the current event;
    • Ontouchevent: Called in the Dispatchtouchevent method to handle the Click event, the return result indicates whether the current event is consumed, and if not consumed, the current view cannot receive the event again in the same sequence of events.
Three ways to relate to each other
publicbooleandispatchTouchEvent(MotionEvent ev) {     booleanfalse;    if(onInterceptTouchEvent(ev)) {         consume = onTouchEvent(ev);    else {         consume = child.dispatchTouchEvent(ev);     }    return consume; }

The pseudo-code above describes the relationship between the three of them well. If the current view interception event is given to its own ontouchevent, otherwise it is passed to the child view until the event is finally processed.

Event Distribution Order

When a click event is generated, it is passed as follows: Activity, Window, View. If the view's Ontouchevent returns false, its parent container ontouchevent will be called, and so on, will eventually be processed by the activity's ontouchevent.

Activity-to-event distribution process

Activity, Window, Decorview.

Windows is an abstract class that controls the appearance and behavior policies of top-level view , and Phonewindow is the only implementation of this class.
Decorview is the underlying container of the current interface, that is, the view set by Setcontentview is a child view of it.

The distribution process for top-level view-to-click events

ViewGroup, Dispatchtouchevent, onintercepttouchevent, OnTouch or ontouchevent

Top view is generally a viewgroup. After intercepting the event, if ViewGroup set the Montouchlistener, the Ontouch method in listener will block out ontouchevent. If Ontouchevent is set to Monclicklistener, the onclick in the listener is called. If ViewGroup is not intercepted, pass it to the child view until the entire event is distributed.

View-to-click event handling Process

If the view is set to Montouchlistener, the Ontouch method in listener will block ontouchevent out. If Ontouchevent is set to Monclicklistener, the onclick in the listener is called.
View does not have a onintercepttouchevent method, and once a click event is passed to him, he will handle it.

Note: The above only describes the principle of the event distribution process, the analysis of the source code to the corresponding section of the reference book.

Welcome reprint, Reprint please indicate the source http://blog.csdn.net/l664675249/article/details/50738102

Event distribution mechanism for view in Android--android developing art exploration notes

Related Article

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.