Android Event distribution mechanism

Source: Internet
Author: User

Learn Android students pay attention to!!!

You are welcome to join the Android Learning Exchange Group number: 364595326 we learn android! together.

The event pass of view in Android is passed from top to bottom, that is, the event is always passed to the parent element first, and then the parent element distributes the event to the child view.

There is an important object in the event distribution pass: Motionevent,motionevent has a method getaction, which returns an int type, and we can judge the event type from this return value, typical of the following three types:

Action_down: When your finger just clicked on the screen

Action_move: Finger moves on the screen

ACTION_UP: Phone left screen

We take a series of events from the touch of a finger to the screen until the finger leaves the screen, which is called the same event sequence, and an event sequence consists of a action_down, several action_move, and a action_up.

The distribution process for events is accomplished in three important ways:

1. Public boolean dispatchtouchevent (Motionevent event)

View distribution event, a view as long as the event can be received, the first thing to do is the method, and the method is bound to execute, here we call it distribution events.

2. Public boolean onintercepttouchevent (Motionevent event)

View intercept event, normally the Dispatchtouchevent distribution event calls the method, the return value of the method is used to determine whether the current view is to intercept the event and returns True, then the Ontouchevent method of the current view is called. And the event is not passed down. Here we need to note that if a view decides to intercept an event, that is, Onintercepttouchevent returns True, the method is not called during execution of the remainder of the same sequence of events. This is what we call an interception event.

If a view, such as TextView, inherits directly from view instead of ViewGroup and cannot add a child view, then the view is not a onintercepttouchevent method, and once an event is passed to it, The Ontoucheevent method will be called unless the settings are not clickable (clickable and longclickable are also set to false); [Note: Activity also has no onintercepttouchevent event]

3. Public boolean ontouchevent (Motionevent event)

View is used to handle a click event, the return value indicates whether the current view consumes the event, if the event is passed to the current view's Ontouchevent method, and the method returns false, then the event is passed up from the current view, and it is all from the top view Ontouchevent to receive and cannot receive the next event. In other words: If you do not consume, that is, return false, then the remaining events in the same sequence of events will not be passed over.

If a view has the Ontouchlistener event set, the Ontouchlistener is executed first, and then the return value is used to determine if the Ontouchevent method is to be executed (returns True if Ontoucheevent is not performed, and vice versa) .

Here is a pseudo-code that can show the relationship of these three events (excerpt from: Android Development Art):

public boolean dispatchtouchevent (Motionevent event) {

Boolean consume = false;

if (Onintercepttouchevent (event)) {

consume = Ontouchevent (event);

}else{

consume = Child.dispatchtouchevent (event);

}

return consume;

}

I believe we all see it very clearly.

Learn Android students pay attention to!!!

You are welcome to join the Android Learning Exchange Group number: 364595326 we learn android! together.


This article from "12568240" blog, declined reprint!

Android Event distribution mechanism

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.