Event distribution mechanism for Android view

Source: Internet
Author: User

1 events are delivered in the order of activity, Window, top view

When the touch event is generated, it is first handled by the activity's dispatchtouchevent

  /*** Called to process touch screens events.  You can override this to * intercept all touch screen events before they is dispatched to the * window.     Be sure to the implementation for touch screens events * that should be handled normally. *     * @paramEV the touch screen event. *     * @returnBoolean Return True if this event is consumed. */     Public Booleandispatchtouchevent (motionevent ev) {if(ev.getaction () = =Motionevent.action_down)        {onuserinteraction (); }        if(GetWindow (). superdispatchtouchevent (EV)) {return true; }        returnontouchevent (EV); }

The event is then passed to the Window's superdispatchtouchevent. If all of the view has no consumption events, it will eventually be handed over to the activity's ontouchevent processing.

2 Window is an abstract class whose only implementation class is Phonewindow

/** * Abstract base class for a top-level windows look and behavior policy.   */  Public Abstract class Window {
}

View Phonewindow's Superdispatchtouchevent

 Public Boolean superdispatchtouchevent (Motionevent event) {            return  mdecor.superdispatchtouchevent ( event);    }

Called the superdispatchtouchevent of Mdecor.

Private Final class extends Implements Rootviewsurfacetaker {} // This is the top-level view of the window, containing the window decor. Private Decorview Mdecor;

Mdecor is Decorview, Decorview is a framelayout, is the parent container of the view we set Setcontentview.

Keep looking at Decorview's superdispatchtouchevent.

 Public Boolean superdispatchtouchevent (Motionevent event) {    returnSuper. Dispatchtouchevent ( event);}

The dispatchtouchevent of the parent class is called, and there is no dispatchtouchevent in Framelayout, and the ViewGroup of Framelayout's parent class dispatchtouchevent is actually called.

The event is passed to the top-level view (ViewGroup), and the event is distributed to the child view from the top-level view.

3 The distribution of events is mainly related to 3 methods

public boolean dispatchtouchevent (Motionevent event)

When the event is passed to a view, the dispatchtouchevent of the view is called to distribute it down

public boolean onintercepttouchevent (motionevent ev)

Interception of events, only ViewGroup have interception method, single view No, the event to a single view directly call Ontouchevent to process

public boolean ontouchevent (Motionevent event)

Handle Click events, True indicates consumption of this event, false means no consumption

4 The following pseudo-code can well describe the distribution process of events

 Public Boolean dispatchtouchevent (motionevent ev) {    booleanfalse;     if (onintercepttouchevent (EV)) {        = ontouchevent (EV);    } Else {        = child.dispatchtouchevent (EV);    }         return consume;}

When the event is passed to the top-level viewgroup, the ViewGroup dispatchtouchevent is called for distribution. If this viewgroup onintercepttouchevent returns True to the table is it to intercept the event, then it will call its ontouchevent for processing. If you do not intercept it will be handed over to its child view to continue distributing, so repeatedly until the event is finally processed.

Normally, an event sequence can be intercepted and consumed by only one view. Once a view has intercepted an event, all events in the same sequence of events will be handed over to it.

If a view's Ontouchevent returns false, then the ontouchevent of its parent container will be called, and so on. If all of the view does not handle this event, the event will eventually be returned to the activity's ontouchevent for processing.

Event distribution details are more complex, but the basic process is the pseudo-code above. Event distribution mechanism is to deal with the root of the sliding conflict, know the principle, encounter problems and look at the source code on the line.

Event distribution mechanism for Android view

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.