Introduction to Android event distribution mechanism (I); Introduction to android

Source: Internet
Author: User

Introduction to Android event distribution mechanism (I); Introduction to android

--- Restore content start ---

I. What is it?

First, we need to know what event distribution is. Generally speaking, when a touch event occurs, it goes from a window to a view and then to a view until it is consumed.

Ii. What to do

In the process of in-depth study of android, we often do not know why we should study these theories. They are boring and difficult to understand, and even do not use these theories when we are at the initial stage of development.

So we need to learn some technologies and know what they can do.

It is relatively simple. How to Deal with sliding conflicts during development.

Iii. Analysis of the event distribution mechanism.

First of all, there are a lot of source code for event distribution in this section. For more information, see Guo Lin's blog, the source code analysis is very clear. Here I will mainly explain some of my own understandings of it.

Let's first get to know three methods.

DispatchTouchEvent (MotionEvent ev) // used for event Distribution

OnInterceptTouchEvent (MotionEvent ev) // called in dispatchTouchEvent () to determine whether to intercept an event

OnTouchEvent (MotionEvent ev) // called in dispatchTouchEvent () to process click events

// Pseudocode Analysis for easy understanding

Public boolean dispatchTouchEvent (MotionEvent ev ){

Boolean consume = false;

If (onInterceptTouchEvent (ev )){

Consume = onTouchEvent (ev );

} Else {

Consum = child. dispatchTouchEvent (ev );

}

Return consum;

}

 

First, we will separate the event distribution mechanism by two protagonists: ViewGroup and View.

For ViewGroup

When a click event is generated, the dispatchTouchEvent () method is called. If the onInterceptTouchEvent () method returns true, the event is intercepted, and false is returned, the event is handed over to the subview to continue the preceding steps until the event is processed.

For View

Handle events, set onTouchEventListener (), onTouch () callback, return false, onTouchEvent () will be called, if setOnClickListener () is set at this time, The onClick () method will be called. If true is returned, onTouchEvent () is not called. If setOnClickListener () is set, The onClick () method is not called. This is the View distribution mechanism.

 

 

Simply put, the above two parts constitute the event distribution mechanism. Since the source code is not analyzed, it looks very simple to draw a flowchart.

After learning this section, I must have a general understanding of android event distribution, but there may be questions, such as why The onClick method is sometimes set and won't be called, I will analyze the source code later.

 

Supplement: 1. After a click event is generated, follow the following sequence: Activity-> Window-> View

2. When no View processes the event (onTouchEvent () returns false), the event is returned to the Activity for processing.

3. onTouch and onTouchEvent are both called in dispatchTouchEvent (). onTouch takes precedence over onTouchEvent. If the onTouch method returns true to consume the event, the onTouchEvent will not be executed. In addition, onTouch requires two prerequisites for execution. The value of the first mOnTouchListener cannot be blank, and the control currently clicked must be enable. Therefore, if you have a control that is not enable, registering an onTouch event for it will never be executed. For this type of control, if we want to listen to its touch event, we must rewrite the onTouchEvent method in the control.

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.