The processing mechanism of Android touch event

Source: Internet
Author: User


First, in view, there are two callback functions (leaf view, can not add control view, such as: Textview,imageview)


public boolean dispatchtouchevent (Motionevent ev);

public boolean ontouchevent (Motionevent ev);



Second, in ViewGroup, there are three callback functions (Layout,adapterview)


public boolean dispatchtouchevent (Motionevent ev);

public boolean onintercepttouchevent (Motionevent ev);

public boolean ontouchevent (Motionevent ev);



Three, in activity, there are two callback functions


public boolean dispatchtouchevent (Motionevent ev);

public boolean ontouchevent (Motionevent ev);


When touchevent occurs, the activity first passes the touchevent to the topmost viewgroup, TouchEvent first to the topmost viewgroup, and then by the Dispatchtouchevent method for distribution.

(generally not overriding dispatchtouchevent, overriding dispatchtouchevent must call super.dispatchtouchevent (EV); otherwise intercepttouchevent will not be called and Ontouchevent, the event is over. )

Returning false indicates that no subsequent events are accepted, so this subsequent operation does not work.

If true, the ViewGroup Intercepttouchevent method is given to determine whether to intercept the event,


If Intercepttouchevent returns True, that is, it is intercepted, it is given to the ontouchevent of the viewgroup intercepting it to handle,

If Intercepttouchevent returns false, it is passed to the child view. The dispatchtouchevent of the child view begins the distribution of this event. If the child view is ViewGroup, repeat the above action.


If the child view is a leaf view,dispatchtouchevent returns true, it means that it needs to handle the event, and the event will be passed to Ontouchevent.

Ontouchevent returns FALSE, the event is passed to the ontouchevent () processing of the view on its previous level. (When you return false, pass it to the parent view);

Ontouchevent () returns True, then subsequent events will continue to be passed to the view's ontouchevent () processing.

Otherwise, if the dispatchtouchevent of the leaf view returns false, it does not have to be processed, and it is not allowed to receive subsequent touch-screen events. Pass the subsequent events up to the parent view's ontouchevent processing









The ontouchevent default value in ViewGroup is false.

The ontouchevent in view returns the default value of True.



The transfer mechanism of the touch event between Onintercepttouchevent () and ontouchevent and each childview depends entirely on the return value of Onintercepttouchevent () and Ontouchevent ().

A return value of TRUE indicates that the event was received and processed correctly, and the return value of FALSE indicates that the event was not processed and will continue to be passed

(Just transfer direction is different, onintercepttouchevent () to the child view, and Ontouchevent () to the parent view).


Handling of Android Touch events

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.