Android ViewGroup Event Distribution mechanism

Source: Internet
Author: User

3, about interception 1, how to intercept

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

Onintercepttouchevent method for Carbon ViewGroup:

[Java]View PlainCopy
  1. @Override
  2. Public Boolean onintercepttouchevent (motionevent ev)
  3. {
  4. int action = Ev.getaction ();
  5. switch (action)
  6. {
  7. Case Motionevent.action_down:
  8. //If you feel you need to intercept
  9. return true;
  10. Case Motionevent.action_move:
  11. //If you feel you need to intercept
  12. return true;
  13. Case MOTIONEVENT.ACTION_UP:
  14. //If you feel you need to intercept
  15. return true;
  16. }
  17. return false;
  18. }


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:

[Java]View PlainCopy
  1. @Override
  2. Public Boolean dispatchtouchevent (Motionevent event)
  3. {
  4. GetParent (). Requestdisallowintercepttouchevent (true);
  5. int action = Event.getaction ();
  6. switch (action)
  7. {
  8. Case Motionevent.action_down:
  9. LOG.E (TAG, "Dispatchtouchevent action_down");
  10. Break ;
  11. Case Motionevent.action_move:
  12. LOG.E (TAG, "Dispatchtouchevent action_move");
  13. Break ;
  14. Case MOTIONEVENT.ACTION_UP:
  15. LOG.E (TAG, "Dispatchtouchevent action_up");
  16. Break ;
  17. Default:
  18. Break ;
  19. }
  20. return super.dispatchtouchevent (event);
  21. }


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:

[Java]View PlainCopy
  1. if (!disallowintercept && onintercepttouchevent (EV)) {
  2. final Float XC = scrolledxfloat-(float) target.mleft;
  3. final Float YC = scrolledyfloat-(float) target.mtop;
  4. Mprivateflags &= ~cancel_next_up_event;
  5. Ev.setaction (Motionevent.action_cancel);
  6. Ev.setlocation (XC, YC);
  7. if (!target.dispatchtouchevent (EV)) {
  8. //target didn ' t handle action_cancel. Much we can do
  9. //But they should has.
  10. }
  11. //Clear the target
  12. Mmotiontarget = null;
  13. //Don ' t dispatch this event to our own view, because we already
  14. //Saw it when intercepting, we just want to give the following
  15. //event to the normal ontouchevent ().
  16. return true;
  17. }


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

[Java]View PlainCopy
  1. if (child.dispatchtouchevent (EV)) {
  2. //Event handled, we have a target now.
  3. Mmotiontarget = child;
  4. return true;
  5. }


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:

[Java]View PlainCopy
  1. Final View target = Mmotiontarget;
  2. if (target = = null) {
  3. //We don't have a target, this means we ' re handling the
  4. //event as a regular view.
  5. Ev.setlocation (XF, YF);
  6. if ((Mprivateflags & cancel_next_up_event)! = 0) {
  7. Ev.setaction (Motionevent.action_cancel);
  8. Mprivateflags &= ~cancel_next_up_event;
  9. }
  10. return super.dispatchtouchevent (EV);
  11. }


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~~

The source code is this:

[Java]View PlainCopy
  1. if (((viewflags & clickable) = = Clickable | |
  2. (ViewFlags & long_clickable) = = long_clickable)) {
  3. 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 off the menu

(EXT) Android ViewGroup event distribution mechanism

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.