Android Event distribution mechanism

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/chziroy/article/details/44401615

To understand the Android event distribution mechanism, first of all have to understand a few concepts, but also as a summary, if temporarily do not understand it is OK, this article will explain these questions.

1, click on the screen, first the event's delivery starts with the activity's Dispatchtouchevent () method.

2, about the Android event distribution mechanism, there are three methods related to the method: Ontouchevent (), Dispatchtouchevent (), and Onintercepttouchevent (), and the related classes have Activity,view , ViewGroup.

3, the time distribution order is dispatchtouchevent---onintercepttouchevent---ontouchevent

4,android event distribution mechanism, there is a downward distribution process , the process mainly called dispatchtouchevent, there is an upward return process , mainly rely on the Ontouchevent method,

The 5,android event is distributed from the parent view to the child view, and if the event is intercepted, the event does not continue to distribute downward and is consumed by the current view. At this point, the downward distribution process above ends prematurely

6, no consumed events, from the parent view to the sub-view, and finally back to the activity, by the activity of ontouchevent () consumption. At this point, the upward return process ends prematurely.


This article does not directly post the relevant methods and classes of the source code, and will be posted its "pseudo-code", easy to understand.


Dispatchtouchevent method

Starting from the beginning of the event distribution, the Dispatchtouchevent () method of the activity,

public boolean dispatchtouchevent (motionevent ev) {        //First step, event distribution        //second step, if the event is not consumed in distribution, The Ontouchevent () method that is passed to the activity}

The first step of the code is "distributing events", where will the event be distributed, distributed to a viewgroup of its root layout (in fact, even if your activity's layout file does not have a root layout set for LinearLayout, The system will also default to you with a default framelayout). Event distribution to the ViewGroup, entered the ViewGroup Dispatchtouchevent method, in view of the source of the method, Android3.0 before the method of the source code and later version of the source code is different. But the principle is roughly the same. The following are the principles of this method.

public boolean dispatchtouchevent (motionevent ev) {call onintercepttouchevent to check if the event is blocked if (no interception) { In ViewGroup, the traversal lookup is currently clicked on which sub-view if (found) {Call The dispatchtouchevent of the child view, recursively go down}else{not found, then pass the event to Ontouchlistener, No listener is passed to Ontouchevent () if again listener or ontouchevent () the Down event returns True, representing the event being consumed, Subsequent moves and up are handled by listener or ontouchevent (), and if the down event returns false, subsequent Move,up events will not go to the viewgroup of this layer, but are consumed directly in the previous view. The  }else{event was intercepted, and the previously clicked sub-view would receive a Action_cancel event, while the down event was passed to Ontouchlistener, and no listener was passed to Ontouchevent (), Still conform to the relationship between the down and Move,up events above}}

In the code above, the recursive invocation of dispatchtouchevent in the child view, if the child view is ViewGroup, still enters the ViewGroup dispatchtouchevent method, as in the code above, if the child view is a view, Enter the dispatchtouchevent of the view, whose code is not the same as the ViewGroup code, as follows

public boolean dispatchtouchevent (motionevent ev) {             //If there is listener, the event is passed to listener     //If there is no listener, The event is passed to Ontouchevent ()}


Onintercepttouchevent method This method only appears in the ViewGroup, the source code is very easy, and its annotations are very simple and brief description of the principle of the method, and even describe the principle of dispatchtouchevent, as follows
 /** * Implement This method to intercept all touch screen motion events. This * allows your to watch events as they is dispatched to your children, and * Take ownership of the current GES     Ture at any point. * * <p>using This function takes some care, as it have a fairly complicated * interaction with {@link View#on TouchEvent (motionevent) * View.ontouchevent (Motionevent)}, and using it requires implementing * that method as wel  L as this one in the correct. Events would be * received in the following order: * * <ol> * <li> You'll receive the down EV     ent here. * <li> the down event is handled either by a child of this view * group, or given to your own ontouchevent ( ) method to handle; This means * should implement ontouchevent () to return true, so you'll * continue to see the rest of the GES  Ture (instead of looking for * A, parent view to handle it).    Also, by returning True from * Ontouchevent (), you'll not receive any following * events in Onintercepttouchevent () and all touch processing mus     T * happen in ontouchevent () like normal.  * <li> for as long as you return false from this function, each following * event (up to and including the final     UP) would be delivered first here * and then to the target ' s ontouchevent (). * <li> If You return true from this, you won't receive any * following events:the target view would receive t He same event but * with the action {@link Motionevent#action_cancel}, and all further * events would be delivered     to your Ontouchevent () method and no longer * appear here.     * </ol> * * @param ev The motion event being dispatched down the hierarchy. * @return return True to steal motion events from the children and has * them dispatched to this viewgroup through on     TouchEvent ().    * The current target would receive an Action_cancel event, and no further * Messages'll is delivered here.    */Public Boolean onintercepttouchevent (Motionevent ev) {return false; }

Ontouchevent method If the Ontouchlistener is set, it will not go into View/viewgroup ontouchevent (), but enter Ontouchlistener. If the down event returns true in a view/viewgroup, subsequent move and up events will continue to come to the current view/viewgroup, otherwise Subsequent events will go to the parent view's Ontouchlistener or ontouchevent, so if all Ontouchlistener or ontouchevent return false for all events, The final event will return to the activity's ontouchevent, so it doesn't seem to matter what the method returns in the activity.

Summarize in fact, the event distribution mechanism, can be divided into two processes, one is the downward distribution process, one is the upward return process, where the downward distribution is by the Dispatchtouchevent method, from the activity of the Dispatchtouchevent Until the dispatchtouchevent of the target view, and then into the ontouchevent of the target view, back up is ontouchevent, from the ontouchevent of the target view to the ontouchevent of the activity.
while the process of distributing downward may end prematurely, this is affected by onintercepttouchevent, or ViewGroup has no child views, both factors can cause the process to end prematurely, leading to an upward return process. The upward return process may also end prematurely, and if the event returns true in Ontouchevent, that is, the event is consumed, then the event does not continue to pass up, and the process ends prematurely.


Word: " two processes, two truncation "


A few of the more excellent blogs

http://codetheory.in/understanding-android-input-touch-events/

Http://www.cnblogs.com/sunzn/archive/2013/05/10/3064129.html#top

Https://gist.github.com/Leaking/16e682b1ffac3a59c3df

Android Event distribution mechanism

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.