Android touch event

Source: Internet
Author: User

There are three events related to touch events in Android:
Public Boolean dispatchtouchevent (motionevent eV): transmits the touch event to the target view (which can be your own ).
Public Boolean onintercepttouchevent (motionevent eV): defined in viewgroup to intercept the transfer of touch events.
Public Boolean ontouchevent (motionevent event): Touch event processing function.

 

Let's talk about two methods of event transmission:
Tunnel mode: pass down from the root element until the sub-element at the inmost layer or stop the transfer due to a certain condition in a certain mona1 element in the middle.
Bubble mode: transfers the child elements from the inmost layer out of sequence until the root element or a mona1 element in the middle stops transmission due to a certain condition.

 

Touch events are passed from top to bottom in a tunnel through dispatchtouchevent. If true is returned when ontouchevent is executed in a view, the next event (action_up after action_down, and several action_moves that may be contained in the middle, from action_down to action_up, is a continuous event, this is what I think, I don't know whether it is accurate) it will still be passed to the ontouchevent of this view. If false is returned, the next event will not be passed to this view, instead, it executes the ontouchevent of its parent view. Every time the ontouchevent event of a view returns false, the next event (if any) will stop at the parent view of the view and rise to a level each time, similar to the bubble mode.

 

when a touch event is transmitted, all elements pass through a view, but the elements at the outermost layer of event processing are not views. Except for the window-related events, when a touch event occurs, the dispatchtouchevent function of the current activity is called before the event is passed to the view element at the lower layer. When the dispatchtouchevent is passed down by a view, if this view is a viewgroup, its onintercepttouchevent function is called. This function indicates whether to intercept touch events. If this function returns true, this viewgroup intercepts the transfer of the event, and the touch event will not be passed down to its subview, but will be processed by it, so it will call its ontouchevent function, if no viewgroup intercepts the event during the transfer process, that is, all viewgroups that pass through return false, the event is finally passed to the innermost view, which is generally a widget, of course, it can also be a viewgroup (which does not contain any elements). If the last event is passed to a view (not a viewgroup ), the ontouchlistener of the view will be called first (if set). If ontouchlistener returns false, the ont of the view will be called again. Ouchevent (true is returned by default). If the last event is passed to a viewgroup (no sub-view), its ontouchevent function is called, and false is returned by default.

If true is returned when the ontouchevent function of a view is called, The next touch event (action_up after action_down, and several action_move actions that may be contained in the middle, from action_down to action_up to a continuous event, this is what you think, do not know whether it is accurate or not) will still be passed to this view and call its ontouchevent function, in the ontouchevent function, different values can be returned according to the condition. If a false value is returned for this function at one time, the next touch event will not be passed to this view, instead, it will terminate in its parent view and call the ontouchevent of its parent view. If the ontouchevent function of all views returns false, the next touch event will be processed by the activity, that is, the ontouchevent function of the activity will be called.

 

when calling the dispatchtouchevent function of viewgroup, The onintercepttouchevent function is called to determine whether an event is intercepted. If no event is intercepted (false is returned ), the dispatchtouchevent function of all sub-views of the viewgroup is called in sequence. For example, if three viewgroups are stacked on a framelayout, The dispatchtouchevent function of the three viewgroups is called in sequence in the dispatchtouchevent event of the framelayout, the dispatchtouchevent of the three viewgroups also calls the dispatchtouchevent function of their sub-views in sequence until the dispatchtouchevent of one of the Views returns true, indicating that the touch event has been processed, you do not need to call the slibling views of this view. For example, when the dispatchtouchevent function of the three stacked viewgroups is called, if the dispatchtouchevent function of the first viewgroup returns true (this event has been consumed ), then the dispatchtouchevent of the other two viewgroups will not be called. You can customize a subclass of viewgroup and reload its dispatchtouchevent function so that it still returns false after processing the touch event, then it will call the dispatchtouchevent function of other view brothers.

Related Article

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.