In Android, events mainly include clicks, long presses, drag, swipe, and so on, which make up the Android event response, and overall, all events are based on the following three sections:
Press (Action_down), Move (Action_move), lift (ACTION_UP). The responses are all based on view and ViewGroup, and the responses are as follows:
In View.java:
Publi boolean dispatchtouchevent (Motionevent event)
public boolean ontouchevent (Motionevent event)
In Viewgroup.java
public boolean dispatchtouchevent (Motionevent event)
public boolean ontouchevent (Motionevent event)
public boolean onintercepttouchevent (Motionevent event)
In the case of component nesting, the response processing for an event is passed from the topmost component to the child component, until the last view component.
You can see that there is an extra onintercepttouchevent method in ViewGroup than in view, because the ViewGroup component can have subcomponents, so you need to intercept decide whether or not to pass the event to the child component.
The functions are specific to the following functions:
Ontouchevent is the place that is really used for business logic processing, returns true to indicate that the event has been consumed, and returns false to indicate that the event continues to pass.
Onintercepttouchevent is used to determine whether an event needs to be intercepted to prevent it from continuing to pass to the subassembly, and returns false to indicate that the Dispatchtouchevent method of the recursive call subassembly does not need to be intercepted. Return true indicates that interception is required, and the Ontouchevent method of this component is called directly for processing.
The above two functions relatively good understanding some, the most important is the third, before on the internet to see a lot, but did not speak particularly clearly. Mostly said to be used for event distribution, return True to not continue distribution, return false to continue distribution. But I've never been clear about the difference between this and the use of onintercepttouchevent.
The direct point is that Android's handling of the touch event is done by hand, and this recursion is reflected in the dispatchtouchevent. The two functions written above are the business logic that is invoked in dispatchtouchevent and executed to achieve its distribution.
It is possible to call three methods in Dispatchtouchevent:
1, the onintercepttouchevent of this component
2, sub-component of the Dispatchtouchevent
3, the ontouchevent of this component
Dispatchtouchevent () Specific execution logic in ViewGroup:
1, first execute the onintercepttouchevent of this component. If False is returned, indicating that no interception is required, the second method, the Dispatchtouchevent method of the subassembly, is called, and if true does not need to be passed to the subassembly, the Ontouchevent method of the component is called directly
2. If you need to pass events to the subassembly in the first step. The Ontouchevent method of this component is called if the dispatchtouchevent of a recursive calling subcomponent returns False, and if true, there is no need to call the Ontouchevent method of this component
3. The return value of the dispatchtouchevent is returned to the parent component's Dispatchtouchevent method, based on the results of the previous two steps.
The dispatchtouchevent in the view will call its own ontouchevent directly.
It is generally not necessary to override the Dispatchtouchevent method, and if you must override it, be careful to call the Super.dispatchtouchevent () method, or the recursive call will stop here.
Without considering dispatchtouchevent, the simple execution process is this:
The topmost component responds to the event first, then passes on to the subassembly, invokes the Onintercepttouchevent method of the subassembly, and returns True to the Onintercepttouchevent method of a component A, or to the view component. The component's Ontouchevent method is then called and then returned to the parent component, calling the parent component's ontouchevent until the Ontouchevent method of a parent component returns TRUE.
It's actually a process that starts from the parent component continuously downward to the onintercepttouchevent, and then calls Ontouchevent from the child component.
It is also important to note that if a component in the process intercepts and processes the Action_down event, then the corresponding Action_move, ACTION_UP, and other events will no longer be passed to his descendant component, but then passed to the component to perform the returned process.
In the process saw an article in the Dispatchtouchevent code commented, you can more clearly explain the problem, as follows, the reference address is: http://blog.csdn.net/hdxiaoyu2/article/details/25563453
@Override public boolean dispatchtouchevent (motionevent ev) {if (minputeventconsistencyverifier! = null) { Minputeventconsistencyverifier.ontouchevent (EV, 1); }//Check for interception. Final Boolean intercepted;//blocked tag variable if (actionmasked = = Motionevent.action_down | | mfirsttouchtarget ! = NULL) {Final Boolean disallowintercept = (Mgroupflags & flag_disallow_intercept)! = 0; if (!disallowintercept) {//calls its own onintercepttochevent to determine if it needs to intercept intercepted = On Intercepttochevent (EV); Ev.setaction (action); Restore action in case it is changed} else {intercepted = false; }} else {///There is no touch targets and the This action is a initial down/so this VI EW Group continues to intercept touches. intercepted = true; }//If not intercepted if (!canceled &&!intercepted) {if (actionmasked = = Motionevent.action_down | | (split && actionmasked = = Motionevent.action_pointer_down) | | actionmasked = = Motionevent.action_hover_move) {if (Newtouchtarget = = NULL && Childrencount! = 0) {final float x = Ev.getx (Actionindex); Final float y = ev.gety (Actionindex); Find a child that can receive the event. Scan children from front to back. Final view[] children = Mchildren; Final Boolean customorder = ischildrendrawingorderenabled (); Iterate through all the child view and call their event distribution method Dispatchtouchevent () for (int i = childrenCount-1; I >= 0; i--) { Final int childindex = Customorder? Getchilddrawingorder (Childrencount, i): i; Final View Child = Children[childindex]; if (!canviewreceivepointerevents (child) | |!istransformedtouchpointinview (x, y, child, NULL )) {continue; } Newtouchtarget = Gettouchtarget (child); Newtouchtarget represents the event pass view target, and when not empty, jumps directly out of the loop if (newtouchtarget! = null) { Newtouchtarget.pointeridbits |= idbitstoassign; Break } resetcancelnextupflag (child); Recursive call Sub View Distribution event method, if (Dispatchtransformedtouchevent (EV, FALSE, Child, Idbitstoassign)) { Mlasttouchdowntime = Ev.getdowntime (); Mlasttouchdownindex = Childindex; Mlasttouchdownx = Ev.getx (); Mlasttouchdowny = Ev.gety (); Set distribution target Newtouchtarget to current View Newtouchtarget = Addtouchtarget (Child, idbitstoassign); If the distribution result of the tag sub-view is true, the following code will not invoke the Ontouch method of the current view, which is why Rule 1 is generated Alreadydispat Chedtonewtouchtarget = true; Break }}}} if (Mfirsttouchtarget = = NULL) {//No touch targets so treat this as an ordinary view. handled = dispatchtransformedtouchevent (EV, canceled, NULL, touchtarget.all_pointer_ids); } else {//Dispatch to touch targets, excluding the new touch target if we already//dispatched to It. Cancel touch targets if necessary. Touchtarget predecessor = NULL; Touchtarget target = Mfirsttouchtarget; while (tarGet! = null) {final touchtarget next = Target.next; If Alreadydispatchedtonewtouchtarget is set to True, the following dispatchtransformedtouchevent is not performed//alreadydispatchedton Ewtouchtarget is determined by the Ontouch return value of the child view, if (Alreadydispatchedtonewtouchtarget && target = = Newtouchtarget) {handled = true; } else {Final Boolean cancelchild = Resetcancelnextupflag (target.child) | | intercepted; Perform its own touch event, if (Dispatchtransformedtouchevent (EV, Cancelchild, target.ch ILD, target.pointeridbits)) {handled = true; } if (Cancelchild) {if (predecessor = = null) {Mfirs Ttouchtarget = Next; } else { Predecessor.next = Next; } target.recycle (); target = next; Continue }} predecessor = target; target = next; } } }
Also see two articles will be good, can be used as a reference:
Http://www.infoq.com/cn/articles/android-event-delivery-mechanism
http://blog.csdn.net/hdxiaoyu2/article/details/25563453