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
- @Override
- Public Boolean onintercepttouchevent (motionevent ev)
- {
- int action = Ev.getaction ();
- switch (action)
- {
- Case Motionevent.action_down:
- //If you feel you need to intercept
- return true;
- Case Motionevent.action_move:
- //If you feel you need to intercept
- return true;
- Case MOTIONEVENT.ACTION_UP:
- //If you feel you 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:
[Java]View PlainCopy
- @Override
- Public Boolean dispatchtouchevent (Motionevent event)
- {
- GetParent (). Requestdisallowintercepttouchevent (true);
- int action = Event.getaction ();
- switch (action)
- {
- Case Motionevent.action_down:
- LOG.E (TAG, "Dispatchtouchevent action_down");
- Break ;
- Case Motionevent.action_move:
- LOG.E (TAG, "Dispatchtouchevent action_move");
- Break ;
- Case MOTIONEVENT.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:
[Java]View PlainCopy
- if (!disallowintercept && onintercepttouchevent (EV)) {
- final Float XC = scrolledxfloat-(float) target.mleft;
- final Float YC = 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 target
- Mmotiontarget = 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
[Java]View PlainCopy
- 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:
[Java]View PlainCopy
- Final View target = Mmotiontarget;
- if (target = = null) {
- //We don't have a target, 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~~
The source code is this:
[Java]View PlainCopy
- if (((viewflags & clickable) = = Clickable | |
- (ViewFlags & 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 off the menu
(EXT) Android ViewGroup event distribution mechanism