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, assuming that the temporary can not understand it is no harm, this article will explain the several problems.

1, click on the screen, first the event's delivery starts from 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 is mainly called dispatchtouchevent, another upward return process , mainly rely on the Ontouchevent method,

The 5,android event is distributed from the parent view to the child view, assuming that 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 will not directly post the relevant methods and classes of the source code, and will be affixed to its "pseudo-code", easy to understand.


Dispatchtouchevent method

Start with the start of the event distribution, the Dispatchtouchevent () method of the activity,

public boolean dispatchtouchevent (motionevent ev) {        //First step, event distribution        //second step, assuming that the event was not consumed in the 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 linearlayout root layout set, The system will also default to you with a default framelayout). When the event is distributed to ViewGroup, it enters the ViewGroup dispatchtouchevent method, and when you view the source code of the method, the source code of the method and the higher version number are different before Android3.0. Just basically the same principle. Here's how the method works.

public boolean dispatchtouchevent (motionevent ev) {call onintercepttouchevent to check if the event is blocked if (no interception) { In ViewGroup, the traversal lookup is now clicked on which sub-view if (found) {Call The dispatchtouchevent of the child view, recursively goes down}else{not found, then passes the event to Ontouchlistener, No listener is passed to ontouchevent () assuming that again listener or ontouchevent () the Down event returns True, representing the event being consumed, Perhaps the move and up are handled by listener or ontouchevent (), assuming that the down event returns false, the Move,up event will not go to the viewgroup of this layer, but is 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 (), The relationship between the down and Move,up events is still adhered to}}

In the above code, regarding the recursive invocation of dispatchtouchevent in the child view, assuming that the child view is ViewGroup, the Dispatchtouchevent method of ViewGroup is still entered, as in the above code, assuming that the child view is a view, Enter the dispatchtouchevent of the view, whose code is not the same as the ViewGroup code, such as the following

public boolean dispatchtouchevent (motionevent ev) {             //Assuming there is listener, the event is passed to listener     //Assuming 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 gaze is very simple and brief description of the principle of the method, and even describes the principle of dispatchtouchevent, For example, the following
 /** * 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; }

The Ontouchevent method assumes that the Ontouchlistener is set and will not enter View/viewgroup's ontouchevent () but into Ontouchlistener. Assuming that the down event returns true in a View/viewgroup, the possibly move and up events will continue to the current view/viewgroup, otherwise Perhaps the event will go to the parent view of Ontouchlistener or ontouchevent, so assume that all Ontouchlistener or ontouchevent return false for all events, The event will eventually return to the activity's ontouchevent, so the fact that the method returns in the activity does not seem to matter.

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, in which 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 enable the process to end prematurely, leading to an upward return process. The upward return process may also end prematurely, assuming that the event returns true in Ontouchevent that the event is consumed, and the event does not continue to pass up, and the process ends prematurely.


Word: " two processes, two truncation "


Several of the best 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.