The distribution and consumption mechanism of Touch events under Android programming

Source: Internet
Author: User

The methods associated with Touch events in Android include:dispatchtouchevent (motionevent ev),onintercepttouchevent (motionevent ev) ,ontouchevent (motionevent ev), controls that respond to these methods include:viewgroup,View,Activity. The corresponding relationship between the method and the control is shown 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

    • If return True, the event is distributed to the current View and consumed by the Dispatchtouchevent method, while the event stops passing down;
    • If return is false, event distribution is divided into two scenarios:
    1. If the current View gets events directly from the activity, the event is returned to the activity's ontouchevent for consumption;
    1. If the current view gets an event from the outer parent control, the event is returned to the parent view's ontouchevent for consumption.
    • If you return the system's default Super. Dispatchtouchevent (EV), the event is automatically distributed to the current View's Onintercepttouchevent method.
    • If Onintercepttouchevent returns true, the event is intercepted and the intercepted event is referred to the current View's ontouchevent for processing;
    • If Onintercepttouchevent returns false, the event is released, the event on the current view is passed to the child view, and then the dispatchtouchevent of the view begins the distribution of the event;
    • super . onintercepttouchevent (EV), The event is intercepted by default,
    • If the event is passed to the current view's Ontouchevent method, and the method returns False, the event is passed up from the current view and is received by the ontouchevent of the upper view, if passed to the ontouchevent above Also returns false, the event "disappears" and the next event is not received.
    • If True is returned, the event is received and consumed.
    • If the return super.ontouchevent (EV) default handles the logic of the event and returns false, the same.

From this table we can see that both ViewGroup and View respond to the three methods associated with the Touch event, and the Activityonintercepttouchevent (motionevent ev)That is, event blocking does not respond. It is also important to note that the Viewdispatchtouchevent (motionevent ev)Andonintercepttouchevent (motionevent ev)The premise of the response is that you can add a child view to the view, and if the current view is already a minimal unit view (such as TextView), you cannot add a child view to the smallest view, and you cannot distribute and intercept the event to the child view, so it has nodispatchtouchevent (motionevent ev)Andonintercepttouchevent (motionevent ev)Onlyontouchevent (motionevent ev)。

First,Touch incident Analysis

?  Event Distribution: public boolean dispatchtouchevent (motionevent ev)

touch event occurred   activity  dispatchtouchevent (motionevent ev) method tunnel mode The dispatchtouchevent (motionevent ev) method that passes an event to the outermost View by passing it from the root element down to the outermost child element or in an intermediate element because a condition stops passing, and by the view Dispatchtouchevent (motionevent ev) method to distribute the event. dispatchtouchevent   Event distribution logic is as follows:

?  Event intercept: public boolean onintercepttouchevent (MotionEvent EV)  

outer View Dispatchtouchevent (motionevent ev) method Return to system default super.dispatchtouchevent (EV) Case, the event is automatically distributed to the current View's Onintercepttouchevent method. < Span style= "margin:0px; The event interception logic for padding:0px ">ONINTERCEPTTOUCHEVENT&NBSP; is as follows:

< 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 provides a clear view of the entire passing process of the event (the layout is viewGroup1 in the outermost layer, nested inside a viewgroup2,viewgroup2 with a button added). It can also be seen that the event is first given to the outermost layout (viewGroup1) and then to the inner layer (VIEWGROUP2), when it is passed to the topmost view (Button), and if the view is already a minimum unit, Then it will be handed over to the view's Ontouchevent handling event, and if the event is not consumed, then the event will be passed on to the back, until no one is handling the event, the event will be lost.



The distribution and consumption mechanism of Touch events under Android programming

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.