Android according to the events of planning Touch allocation and consumption mechanism

Source: Internet
Author: User

The methods associated with Touch events in Android include:dispatchtouchevent (motionevent ev),onintercepttouchevent (motionevent ev) ,ontouchevent (motionevent ev); controls that can respond to these methods include:viewgroup,View,Activity. The corresponding relationship of the method to the control, as seen in the following table:

Touch Event-related methods Method functions
ViewGroup
View
Activity
public boolean dispatchtouchevent (motionevent ev) Event distribution
Yes Yes Yes
public boolean onintercepttouchevent (motionevent ev)
Event interception
Yes Yes No
public boolean ontouchevent (motionevent ev) Incident response
Yes Yes Yes

    • Assuming return True, the event is distributed to the current View and consumed by the Dispatchtouchevent method. At the same time the event will stop passing down.
    • Assume return False. Event distribution is divided into two scenarios:
    1. Assume that the current View gets events directly from the Activity. The event is returned to the Activity's ontouchevent for consumption;
    1. Assuming that the current view gets an event from the outer parent control, the event is returned to the parent view's ontouchevent for consumption.

    • Assume that you return the system's default Super. dispatchtouchevent (EV). The event will be distributed to the current View's Onintercepttouchevent method on its own initiative.

    • Assuming that Onintercepttouchevent returns true, the event is intercepted. And the interception of the incident to the current View of the ontouchevent to handle;
    • Assuming that Onintercepttouchevent returns false, the event is released, and the event on the current view is passed to the child view. Then the dispatchtouchevent of the child View to start the distribution of the event;
    • assuming onintercepttouchevent back  super . onintercepttouchevent (EV), The event is intercepted by default,
    • Assume that the event is passed to the current View's Ontouchevent method. Instead, the method returns false. Then this event will be passed up from the current View. And all are received by the ontouchevent of the upper View, assuming that the ontouchevent passed to the above also returns false, the event "disappears" and the next event is not received.
    • If True is returned, the event is received and consumed.
    • Suppose the return super.ontouchevent (EV) handles the logic of the event by default and returns False when the same.

From this table we can see that ViewGroup and View respond to the three methods associated with the Touch event, while Activity is blocking the onintercepttouchevent (motionevent ev) event Line Response.

The

also needs attention is the View to     and  onintercepttouchevent (motionevent ev)   's response is premised on the ability to add a child view to the view, assuming that the current view is already a minimal unit view (for example, TextView), you cannot add a child view to this minimum view, and you cannot distribute and intercept events to the child view. So it has no dispatchtouchevent ( Motionevent ev)   and   onintercepttouchevent (motionevent ev) , only  ontouchevent (motionevent ev) .

First,Touch incident Analysis

?  Event Distribution: public boolean dispatchtouchevent (motionevent ev)

when the Touch event occurs the Activity's dispatchtouchevent (motionevent ev) method is tunneled (passed from the root element down to the most inner child element or in an intermediate element because a condition stops passing) The event is passed to the dispatchtouchevent (motionevent ev) method of the outermost view, and the event is distributed by the view's dispatchtouchevent (Motionevent ev) method.

The event distribution logic for dispatchtouchevent such as the following:

?  Event intercept: public boolean onintercepttouchevent (MotionEvent EV)  

outer View Dispatchtouchevent (motionevent ev) method Return to system default super.dispatchtouchevent (EV) The case. The event will be distributed to the current View's Onintercepttouchevent method on its own initiative. < Span style= "margin:0px; padding:0px ">ONINTERCEPTTOUCHEVENT&NBSP; the event interception logic for such as the following:

< Span style= "margin:0px; padding:0px ">?&NBSP; Event response: public boolean ontouchevent (motionevent ev)

The Dispatchtouchevent returns super.dispatchtouchevent (EV) and onintercepttouchevent returns TRUE or returns Super.onintercepttouchevent ( EV), the ontouchevent will be called.

The event response logic for ontouchevent is as follows:

The following diagram can clearly see the entire passing process of the event (the layout is viewGroup1 in the outermost layer, nested inside a viewGroup2. A button is added to the viewGroup2).

It can also be seen that events are first mapped to the outermost layout (viewGroup1). It is then passed to the inner layer (VIEWGROUP2) in turn, when passed to the most inner view (Button). Assuming that the view is already a minimal unit, it is handed to the view's Ontouchevent handling event. Assuming that the event is not consumed, the event is passed on to the last pass until no one has handled the event and the event is lost.



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Android according to the events of planning Touch allocation and consumption 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.