Event distribution mechanism of View in Android -- Android development art exploration notes

Source: Internet
Author: User

Event distribution mechanism of View in Android -- Android development art exploration notes
Introduction

Event distribution of click events refers to the distribution process of MotionEvent events. When a MotionEvent is generated, the system needs to pass the event to a specific View, the transfer process is the distribution process.

The three methods involved are dispatchTouchEvent, which is used to distribute events. If an event can be passed to the current View, this method will be called, the returned result is affected by the onTouchEvent method of the current View and the dispatchTouchEvent method of the View, indicating whether the current event onInterceptTouchEvent is consumed: used to determine whether to intercept an event. If the current View intercepts an event, in the same event sequence, this method will not be called again. The returned result indicates whether to intercept the current event. onTouchEvent: called in the dispatchTouchEvent method to process click events, the returned result indicates whether the current event is consumed. If the event is not consumed, the current View cannot receive the event again in the same event sequence. Relationship between the three methods
public boolean dispatchTouchEvent(MotionEvent ev) {     boolean consume = false;    if(onInterceptTouchEvent(ev)) {         consume = onTouchEvent(ev);    } else {         consume = child.dispatchTouchEvent(ev);     }    return consume; }

The above Pseudo Code describes the relationship between the three. If the current View intercepts an event, it is handed over to its onTouchEvent for processing. Otherwise, it is passed to the subview until the event is finally processed.

Event distribution order

When a click event is generated, the transfer process is as follows: Activity-> Window-> View. If the onTouchEvent of View returns false, its parent container onTouchEvent will be called, and so on, and will be processed by the onTouchEvent of Activity.

Activity event distribution process

Activity-> Window-> DecorView.

Windows is an abstract class that can be controlledTop ViewPhoneWindow 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 subview of it.

Top-level View distribution of click events

ViewGroup-> dispatchTouchEvent-> onInterceptTouchEvent-> onTouch or onTouchEvent

A top-level View is generally a ViewGroup. After an event is intercepted, if mOnTouchListener is set in ViewGroup, The onTouch method in Listener will block onTouchEvent. If mOnClickListener is set for onTouchEvent, The onClick in Listener is called. If the ViewGroup is not blocked, it is passed to the subview until the entire event is distributed.

View processing of click events

If mOnTouchListener is set for the View, the onTouch method in the Listener will block the onTouchEvent. If mOnClickListener is set for onTouchEvent, The onClick in Listener is called.
View does not have the onInterceptTouchEvent method. Once a click event is passed to it, it will handle it.

Note: The above only describes the principles of the event distribution process. For source code analysis, refer to the relevant chapter in this document.

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.