android-Event Distribution (ViewGroup)

Source: Internet
Author: User

http://blog.csdn.net/guolin_blog/article/details/9153747

http://blog.csdn.net/lmj623565791/article/details/39102591

The last article on the view of the event distribution, this article is mainly ViewGroup

First, let's explore, what is ViewGroup? What is the difference between it and a normal view?

As the name implies, ViewGroup is a set of view that contains a lot of sub-view and sub-vewgroup, which is the parent or indirect parent of all layouts in Android, such as LinearLayout, Relativelayout and so on are inherited from the viewgroup. But ViewGroup is actually a view, but compared to view, it has more features that can contain sub-view and define layout parameters. The ViewGroup inheritance structure is as follows:

As you can see, the various layouts that we often use in our projects are all part of the ViewGroup subclass.

From the Android-event distribution Mechanism Framework overview can be known that ViewGroup returns True is consumption, super is the judgment intercept, false is returned dispatchtouchevent

Source details can be seen here: http://blog.csdn.net/lmj623565791/article/details/39102591

The whole logic is this:

1, Action_down, ViewGroup capture the event, and then determine whether to intercept, if not intercept, then find a child view containing the current x, y coordinates, assign to Mmotiontarget, and then call Mmotiontarget.dispatchtouchevent

2, Action_move, ViewGroup capture the event, and then determine whether to intercept, if there is no interception, the direct call mmotiontarget.dispatchtouchevent (EV)

3, ACTION_UP, ViewGroup capture the event, and then determine whether to intercept, if there is no interception, the direct call mmotiontarget.dispatchtouchevent (EV)

Of course, before the distribution will be modified under the coordinate system, the current x, y minus Child.left and Child.top respectively, and then passed to child;

1. How to intercept

The above summary is based on: if not intercepted, then how to intercept it?

Onintercepttouchevent method for Carbon ViewGroup:

 Public Booleanonintercepttouchevent (motionevent ev) {intAction =ev.getaction (); Switch(action) { CaseMotionevent.action_down://If you feel the need to intercept            return true ;  CaseMotionevent.action_move://If you feel the need to intercept            return true ;  Casemotionevent.action_up://If you feel the need to intercept            return true ; }                    return false; }

Default is not intercept, that is, return false; If you need to intercept, just return to true, this event will not be passed to the child view, and if you are down Retrun true, then down,move,up child view will not capture the event If you return true on move, the child view will not capture events on move and up.

The reason is simple, when onintercepttouchevent (EV) return True, the mmotiontarget is set to null;

2, how not to be intercepted

If the ViewGroup onintercepttouchevent (EV) return True when Action_move, the move of the child view and the up event are intercepted;

What happens when the child view wants to still be able to respond to move and up?

Android gives us a way: Requestdisallowintercepttouchevent (Boolean) is used to set whether or not to allow interception, which we write directly in the dispatchtouchevent of the Child view:

 Public Booleandispatchtouchevent (Motionevent event) {getParent (). Requestdisallowintercepttouchevent (true); intAction =event.getaction (); Switch(action) { Casemotionevent.action_down:log.e (TAG,"Dispatchtouchevent Action_down");  Break;  Casemotionevent.action_move:log.e (TAG,"Dispatchtouchevent Action_move");  Break;  Casemotionevent.action_up:log.e (TAG,"Dispatchtouchevent action_up");  Break; default:               Break; }          return Super. Dispatchtouchevent (event); } 

GetParent (). Requestdisallowintercepttouchevent (True); This allows the child view to capture the move and the up event even if the ViewGroup return true at the time of the move.

From the source can also explain:

The source code for ViewGroup move and up interception is this:

if(!disallowintercept &&onintercepttouchevent (EV)) {              Final floatXC = Scrolledxfloat-(float) Target.mleft; Final floatYC = Scrolledyfloat-(float) Target.mtop; Mprivateflags&= ~cancel_next_up_event;              Ev.setaction (Motionevent.action_cancel);              Ev.setlocation (XC, YC); if(!target.dispatchtouchevent (EV)) {                  //target didn ' t handle action_cancel. Much we can do//But they should has.             }              //Clear the targetMmotiontarget =NULL; //Don ' t dispatch this event to our own view, because we already//saw it when intercepting, we just want to give the following//event to the normal ontouchevent ().             return true; }

When we set the disallowintercept to True, the!disallowintercept is directly false, so the intercepted method body is skipped ~

Note: If ViewGroup in the onintercepttouchevent (EV) Action_down directly return true, then the child view is a method of wood capture events ~ ~ ~

4. If no suitable sub-view is found

Our example, directly click on the button within the ViewGroup, of course, the direct and smooth completion of the entire process;

But there are two kinds of special cases.

1, Action_down, the sub-view.dispatchtouchevent (EV) returned to false;

If you look closely, you'll notice that ViewGroup's dispatchtouchevent (EV) Action_down code is like this

if (child.dispatchtouchevent (EV)) {            //  Event handled, we have a target now.            Mmotiontarget = Child ;             return true ;  }

Only if the child.dispatchtouchevent (EV) returns True will the view be found to handle the current event, that is, mmotiontarget = child;

But if it returns false, then Mmotiontarget is still null

What happens if Mmotiontarget is null?

In fact, ViewGroup is also the sub-class of view, if not found to handle the event of the child view, or simply no child view;

Then, as a view, it is equivalent to the view of the event forward ~ ~ Direct super.dispatchtouchevent (EV);

The source code is this:

FinalView target =Mmotiontarget; if(target = =NULL) {             //we don ' t have a target and this means We ' re handling the//event as a regular view. ev.setlocation (XF, YF); if((Mprivateflags & cancel_next_up_event)! = 0) {ev.setaction (motionevent.action_cancel); Mprivateflags&= ~cancel_next_up_event; }             return Super. dispatchtouchevent (EV); }

We do not have a target element that can handle the event, meaning we need to deal with it ourselves ~ ~ ~ is equivalent to the traditional view~

2, then when the view.dispatchtouchevent (EV) returned to True

If you read the previous blog carefully, you will find that as long as the Sub view supports clicking or long pressing the event must return true~~

This is the source code.

if ((viewflags & clickable) = = Clickable | | & long_clickable) = = long_clickable) {                  returntrue;                   
5. Summary

About the code process has been summarized above ~

1, if ViewGroup found to be able to handle the event of the view, then directly to the child view processing, their own ontouchevent will not be triggered;

2, can through the carbon onintercepttouchevent (EV) method, intercept the child view of the event (that is, return true), the event to their own processing, will execute their own corresponding Ontouchevent method

3. Child view can be called by GetParent (). Requestdisallowintercepttouchevent (True); Prevents ViewGroup from intercepting its move or up event;

OK, so what are the problems that can be solved in practical application?

For example, you need to write a slidingmenu-like left hidden menu, a button on the main activity, a ListView, or any view that responds to a click, your doom on the current view, and the menu bar. Because the Move Event quilt view is processed ~ You need to do this: in ViewGroup's dispatchtouchevent to determine whether the user wants to display the menu, and if so, the event that intercepts the child view in the Onintercepttouchevent (EV) and handle it yourself so that your ontouchevent can show the menu bar well.

android-Event Distribution (ViewGroup)

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.