Quickly grasp the Touch event distribution mechanism in Android development

Source: Internet
Author: User
Tags touch

There are only two protagonists in the touch event distribution: ViewGroup and view. The activity of the touch event is in fact called its internal viewgroup touch events, can be directly treated as viewgroup.

View in ViewGroup, ViewGroup can also be in other viewgroup, this time the internal ViewGroup as view to analyze.

There are three related events in ViewGroup: Onintercepttouchevent, Dispatchtouchevent, Ontouchevent. There are only two related events in view: Dispatchtouchevent, Ontouchevent.

First of all, analyze the process of ViewGroup: First of all, there is a structure model concept: ViewGroup and view constitute a tree structure, the topmost layer of the activity of the viewgroup, there are several viewgroup nodes, There are several ViewGroup nodes or view nodes under each node, and so on. As shown in figure:

When a touch event (a touching event is an example) arrives at the root node, that is, the viewgroup of the acitivty, it is issued sequentially, and the process is implemented by calling the Dispatchtouchevent method of the child View (ViewGroup). In simple terms, it's the viewgroup that iterates through its contained view, calls each view's Dispatchtouchevent method, and when the child view is ViewGroup, it calls the Viwgroup's The Dispatchtouchevent method continues to invoke the Dispatchtouchevent method of its internal view. The order of messages in the above example is this: ①-②-⑤-⑥-⑦-③-④. The Dispatchtouchevent method only handles the distribution of events, it has a Boolean return value, and when returned to true, the order is interrupted. In the example above, if ⑤ 's Dispatchtouchevent returns True, then ⑥-⑦-③-④ will not receive this touch event. To a simple version of the code to deepen understanding:

The code is as follows Copy Code
/**
* ViewGroup
* @param ev
* @return
*/
public boolean dispatchtouchevent (Motionevent ev) {
..//Other processing, in this matter
View[] Views=getchildview ();
for (int i=0;i<views.length;i++) {
Judge the touch to the point on the screen above the child view
if (...) {
if (views[i].dispatchtouchevent (EV))
return true;
}
}
.//Other processing, in this matter
}
/**
* View
* @param ev
* @return
*/
public boolean dispatchtouchevent (Motionevent ev) {
..//Other processing, in this matter
return false;
}

As can be seen here, the dispatchtouchevent of ViewGroup is really in the "distribution" work, and the view of the Dispatchtouchevent method, does not perform the distribution work, or that it distributes the object is their own, To decide whether to hand over the touch event to yourself, and the way to deal with it is ontouchevent event, in fact the Dispatchtouchevent method of the child view actually executes the code like this

The code is as follows Copy Code
/**
* View
* @param ev
* @return
*/
public boolean dispatchtouchevent (Motionevent ev) {
..//Other processing, in this matter
Return Ontouchevent (event);
}

In general, we should not rewrite the Dispatchtouchevent method in normal view because it does not perform the distribution logic. When the touch event arrives in view, what we should do is whether to handle it in the Ontouchevent event.

When did the ontouchevent incident of ViewGroup be handled? When ViewGroup all of the child view returns false, the Ontouchevent event executes. Because ViewGroup is inherited from view, it is also a way to perform ontouchevent events by calling the view's Dispatchtouchevent method.

In the current situation, it seems as if we were to return all the ontouchevent to false so that all of the child controls would be able to respond to this touch event. However, it must be stated that the touch events here are limited to acition_down events, that is, touching press events, while ACITON_UP and Action_move do not. In fact, a complete touch event should consist of one down, one up, and several move. The down way is distributed through the dispatchtouchevent to find a view that really needs to handle the full touch request. When a view or ViewGroup Ontouchevent event returns True, it means that it is the view that is really going to handle this request, and then the ACITON_UP and Action_move will be processed by it. When all the ontouchevent of a child view returns false, this touch request is handled by the root viewgroup, the activity itself.

Look at the improved Dispatchtouchevent method of ViewGroup

The code is as follows Copy Code

View mtarget=null;//save capture touch Event handling view
public boolean dispatchtouchevent (Motionevent ev) {

//.... Other processing, in this matter

if (Ev.getaction () ==keyevent.action_down) {
Each down event is set to NULL

if (!onintercepttouchevent ()) {
Mtarget=null;
View[] Views=getchildview ();
for (int i=0;i<views.length;i++) {
if (views[i].dispatchtouchevent (EV))
Mtarget=views[i];
return true;
}
}
}
ViewGroup itself handles when the child view does not capture the down event. The touch events handled here include down, up, and move
if (mtarget==null) {
return super.dispatchtouchevent (EV);
}
//... Other processing, in this matter
if (Onintercepttouchevent ()) {

//... Other processing, in this matter
}
This step is not performed in the Action_down, and only move and up will be executed.
return mtarget.dispatchtouchevent (EV);

}

ViewGroup also has a onintercepttouchevent, see the name to know this is an interception event. This interception event needs to be explained in two ways:

1. If we return true to the touch event of action as down in a viewgroup onintercepttouchevent, that means that all of the ViewGroup's operations are intercepted. Mtarget will always be null because Mtarget is assigned in the down event. The Ontouchevent event for the ViewGroup is executed because the mtarge is null. In this case, the viewgroup can be treated directly as a view.

2. If we are in a viewgroup onintercepttouchevent, the touch event acion down is returned false, the other returns True, in which case the down event is normally distributed, If the child view returns false, the Mtarget is still empty and has no effect. If a child view returns True,mtarget is assigned, a Mtarget action_delete is distributed to motionevent when Action_move and aciton_up are distributed to the ViewGroup, The value of the mtarget is also emptied so that the next action_move (if the previous operation is not up) will be handled by the ViewGroup ontouchevent.

The situation is used more than two people have not found the use of the scene.

Summarize from beginning to end:

There are only two main characters in the 1.Touch event distribution: ViewGroup and view. ViewGroup contains three related events, including Onintercepttouchevent, Dispatchtouchevent, and Ontouchevent. View contains Dispatchtouchevent, ontouchevent two related events. Among them, ViewGroup inherits from view.

2.ViewGroup and view make up a tree-like structure in which the root node is a viwgroup contained within the activity.

3. Touch event consists of Action_down, Action_move, aciton_up, one complete touch event, down and up are only one, move has several, can be 0.

4. When acitivty receives the touch event, it traverses the child view for distribution of the down event. The traversal of the viewgroup can be seen as recursive. The purpose of the distribution is to find the view that really deals with this complete touch event, which returns true in the ontouchuevent result.

5. When a child view returns true, the distribution of the down event is aborted and the child view is recorded in ViewGroup. The next move and up event is processed directly by the child view. Because the child view is stored in the ViewGroup, the hierarchical ViewGroup node structure, the superior viewgroup save is the ViewGroup object of the view where the event is actually handled: In the structure of Viewgroup0-viewgroup1-textview, TextView returns True, it is saved in ViewGroup1, and ViewGroup1 returns True and is saved in ViewGroup0. When move and up events come, they are passed from ViewGroup0 to ViewGroup1 and then ViewGroup1 to TextView.

6. When all child view in ViewGroup does not capture the down event, the ViewGroup itself's Ontouch event is triggered. The way to trigger is to call the Super.dispatchtouchevent function, the Dispatchtouchevent method of the parent view. Triggers the acitivity ontouchevent method in cases where none of the child view is processed.

7.onInterceptTouchEvent has two functions: 1. Intercept the distribution of down events. 2. Abort the up and move events to the target view, allowing the viewgroup of the target view to capture the up and move events.

In addition, the code listed above is not the real source, but only the source in the event distribution processing of the core processing process, the real source code you can go to see, contains a richer content.

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.