Android event distribution mechanism

Source: Internet
Author: User

Android event distribution mechanism

 

To understand the Android event distribution mechanism, you must first understand several concepts and summarize them. If you cannot understand them for the moment, this article will explain these issues.

1. Click the screen. First, the event is transmitted from the dispatchTouchEvent () method of the Activity.

2. There are three methods for Android event distribution: onTouchEvent (), dispatchTouchEvent (), and onInterceptTouchEvent (). The related classes include Activity, View, and ViewGroup.

3. the distribution order of time is dispatchTouchEvent --- onInterceptTouchEvent --- onTouchEvent

4. Android event distribution mechanism, which hasDownstream distribution processThis process mainly calls dispatchTouchEvent.Upward return processMainly relies on the onTouchEvent method,

5. Android events are distributed from the parent View to the Child view. If an event is intercepted, the event will not be distributed downstream, but will be consumed by the current view. In this caseDownstream distribution processEarly termination

6. events that are not consumed are distributed to the Child view step by step from the parent view, and finally return to the Activity, which is consumed by onTouchEvent () in the Activity. In this caseUpward return processEarly termination

 

This article will not directly post the source code of the above-mentioned methods and classes, but will post its "pseudo code" for ease of understanding.

 

DispatchTouchEvent Method

Start from the starting point of event distribution, that is, the dispatchTouchEvent () method of the Activity,

 

Public boolean dispatchTouchEvent (MotionEvent ev) {// The first step is to distribute the event // The second step. If the event is not consumed during distribution, it is passed to the onTouchEvent () method of the Activity}

 

The first step of the above code is "distributing events". Where will the events be distributed, A ViewGroup that is distributed to its root layout (even if the layout file of your activity does not set a root layout such as LinearLayout, the system will add a default FrameLayout to you by default ). When the event is distributed to ViewGroup, it enters the dispatchTouchEvent method of ViewGroup. When you view the source code of this method, the source code of this method before Android3.0 is different from the source code in a later version. However, the principle is roughly the same. The principles of this method are as follows.

 

Public boolean dispatchTouchEvent (MotionEvent ev) {call onInterceptTouchEvent to check whether an event is intercepted if (no interception is performed) {search through ViewGroup to find the subview that is currently clicked if (found) {call the dispatchTouchEvent of this subview, recursively go down} else {not found, the event is sent to onTouchListener, if there is no Listener, it is passed to onTouchEvent () if there is another listener or onTouchEvent () if the "down" event returns true, it indicates that the event is consumed, and the subsequent moves and up are processed by Listener or onTouchEvent (). If the "down" event returns false, the subsequent moves, the up event will not go to the Viewgroup at this layer, but will be consumed directly at the previous layer. } Else {the event is intercepted. The previously clicked sub-view will receive an ACTION_CANCEL event, and the down event will be sent to onTouchListener. If there is no Listener, it will be sent to onTouchEvent (), still following the above down, move, and up events }}

In the above Code, the recursive call of dispatchTouchEvent in the subview still enters the dispatchTouchEvent method of the ViewGroup if the subview is ViewGroup. Like the above Code, if the subview is View, the dispatchTouchEvent of the View. The code is not the same as that of the ViewGroup, as shown below:

 

 

Public boolean dispatchTouchEvent (MotionEvent ev) {// if there is a 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 ViewGroup, the source code is very easy, and its comments briefly describe the principle of this method, and even describe the principle of dispatchTouchEvent, as shown below
 /**     * Implement this method to intercept all touch screen motion events.  This     * allows you to watch events as they are dispatched to your children, and     * take ownership of the current gesture at any point.     *     * 

Using this function takes some care, as it has a fairly complicated * interaction with {@ link View # onTouchEvent (MotionEvent) * View. onTouchEvent (MotionEvent)}, and using it requires implementing * that method as well as this one in the correct way. events will be * unbound ed in the following order :**

  1. *
  2. You will receive the down event here .*
  3. The down event will be handled either by a child of this view * group, or given to your own onTouchEvent () method to handle; this means * you shoshould implement onTouchEvent () to return true, so you will * continue to see the rest of the gesture (instead of looking for * a parent view to handle it ). also, by returning true from * onTouchEvent (), you will not receive any following * events in onInterceptTouchEvent () and all touch processing must * happen in onTouchEvent () like normal. *
  4. For as long as you return false from this function, each following * event (up to and including the final up) will be delivered first here * and then to the target's onTouchEvent (). *
  5. If you return true from here, you will not receive any * following events: the target view will receive the same event but * with the action {@ link MotionEvent # ACTION_CANCEL }, and all further * events will be delivered to your onTouchEvent () method and no longer * appear here. *
** @ Param ev The motion event being dispatched down the hierarchy. * @ return Return true to steal motion events from the children and have * them dispatched to this ViewGroup through onTouchEvent (). * The current target will receive an ACTION_CANCEL event, and no further * messages will be delivered here. */public boolean onInterceptTouchEvent (MotionEvent ev) {return false ;}
If onTouchEvent method is set to onTouchListener, The onTouchEvent () of the View/ViewGroup is not entered, but the onTouchListener is entered. If the down event returns true in a View/ViewGroup, the subsequent move and up events will continue to the current View/ViewGroup. Otherwise, subsequent events will go to the onTouchListener or onTouchEvent of the parent view. Therefore, if all onTouchListener or onTouchEvent returns false to all events, the final event will return to the onTouchEvent of the Activity, in fact, it does not matter what the method returns in the Activity.

Summary In fact, the event distribution mechanism can be divided into two processes: one is the downward distribution process and the other is the upward return process. The downward distribution relies on the dispatchTouchEvent method, from the dispatchTouchEvent of the Activity to the dispatchTouchEvent of the target view, then, go to the onTouchEvent of the target view. If the returned result is an onTouchEvent, from the onTouchEvent of the target view to the onTouchEvent of the Activity.
However, the downward distribution process may end early, which is affected by onInterceptTouchEvent, or because ViewGroup does not have a subview. In both cases, the process can be terminated early, the process is returned in advance. The upward return process may also end early. If the event returns true in onTouchEvent, that is, the event is consumed, the event will not continue to be passed up, and the process ends early.

 

In a word:"Two procedures, two Truncation"

 

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.