Android Event Distribution

Source: Internet
Author: User

ViewGroup distribution, interception-and processing  

In the event distribution process, there are three main methods involved:

    • Dispatchtouchevent (Motionevent event);

    • Onintercepttouchevent (Motionevent event);

    • Ontouchevent ();

Distribution Processing Flow:

1, in the dispatchtouchevent, first call ViewGroup own Onintercepttouchevent method, determine whether to intercept.

2, if this time to intercept themselves, then call their own Ontouchevent method, if the Ontouchevent method returns True, then the event is consumed, event delivery ends, if the return of false, To prove that this motionevent is not consumed this time, the incident will go up and be dealt with at the top level.

3, if the current ViewGroup Onintercepttouchevent returned false, then it will call its child view of the Dispatchtouchevent method, so that the event is passed down, if its sub-view cannot handle, Then it will come back to call ViewGroup's Ontouchevent method.

View

View is different from ViewGroup, there is no Onintercepttouchevent method in view, because view as the last level of event processing, do not need to determine whether to intercept, it is necessary to intercept, regardless of can be processed, you have to try, So the call process in view is: dispatchtouchevent---ontouchevent

Attention:

The Enable state of view is not related to ontouchevent, only the clickable state is affected by ontouchevent.

Setting view's enable to False will also set the view's clickable to false, but setting the view's Onclicklistener will turn the view clickable to true. So the final solution is to replace the two lines of code in order, the problem is solved.

Gesuturedetector Handling Events

Gesturedetector is a gesture-assisted detection class that detects multiple gestures by default:

Ontouchlistener ontouchevent Onclicklistener

We must know Onclicklistener and Ontouchlistener before we know ontouchevent, and they are all consumers of the event, Ontouchlistener is in the Ontouch method, and Ontouch to Ontouchevent first, that is, once the Ontouchlistener is set and the last Ontouch method returns True, the ontouchevent will not be executed again. and Onclicklistener and ontouchevent have some relationship, Ontouchevent's default implementation will call Onclicklistener's OnClick method, if the ontouchevent is overridden, Because Onclicklistener accepts no action_down and action_up, then setting up Onclicklistener will no longer take effect, this time the click or long press processing can only be handled in Ontouchevent itself.

Conclusion:

1, if the event is not intercepted, the entire event flow will be a U-state.

Activity---->viewgroup--->view from top to bottom call dispatchtouchevent method, until the leaf node (view), then by the view--->viewgroup---> Activity calls the Ontouchevent method from the bottom up.

2, Dispatchtouchevent and ontouchevent once return true, the event stops passing (reaching the end)

3, Dispatchtouchevent, and Ontouchevent return False when the event is passed back to the parent control's ontouchevent processing.

4, Dispatchtouchevent, Ontouchevent, onintercepttouchevent
The default implementation of these methods of ViewGroup and view is to let the entire event install U-shape complete, so return super.xxxxxx () will let the event follow the direction of the U-shape complete the entire event flow path), do not make any changes in the middle, do not backtrack, do not terminate, Every step of the way.

Summarize:

    • For Dispatchtouchevent,ontouchevent,return True is the end event delivery. Return False is the Ontouchevent method that is traced back to the parent view.
    • ViewGroup want to distribute themselves to their ontouchevent, need the Interceptor Onintercepttouchevent method return true to intercept the event.
    • The ViewGroup Interceptor Onintercepttouchevent is not intercepted by default, so return Super.onintercepttouchevent () =return false;
    • View does not have interceptors, in order for view to distribute events to its own ontouchevent,view, the dispatchtouchevent default implementation (super) is to distribute the event to its own ontouchevent.

For Action_move, action_up Summary: Action_down event in which control consumption (return True), then Action_move and Action_ Up will be sent from the top down (through the dispatchtouchevent) to do the event distribution down, will only upload to this control, will not continue to preach, if the Action_down event is dispatchtouchevent consumption, then the event stops delivery, If the Action_down event is consumed in Ontouchevent, the Action_move or Action_up event is passed to the control's ontouchevent processing and the pass is ended.

Android Event Distribution

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.