Android event handling mechanism

Source: Internet
Author: User

When touching the screen,

- -First call the dispatchtouchevent function in Activity , distribute the event, and find the first child control where the click is located Viewgroup1,

--then pass the event aViewgroup1of thedispatchtoucheventfunction, and then look atViewgroup1of theonintercepttouchevent ()whether to intercept,ture, intercept, do not pass down. false, intercept, pass down, becauseViewGroupalso includes the sub -View, so the default return value isfalse, that is, do not intercept thisAction_downevent.

-- then find his child control viewgroup3, repeatedly inferred.

-- Assuming there are no previous interception, then find button1 's dispatchtouchevent () function. The response of the touch event at the bottom .

The pass-through of the touch event is passed from top to bottom (tunnel mode).

Tunnel mode, which is passed from the root element down to the most inner child element or in an intermediate element because a condition stops passing.

The delivery of an event is passed from bottom to top (bubbling mode).

Event is passed tobutton1of theontoucheventfunction Processing. Default returnture. Thenbutton1of thedispatchtoucheventreturntrue, and thenViewGroupof thedispatchtoucheventreturntrue, and finallyActivityof thedispatchtoucheventreturntrue.

Note 1: ( dispatchtouchevent function of view and viewgroup ):

Button1 is a view, and his parent controls are ViewGroup, their Dispatchtouchevent () is not the same .

Viewgroup dispatchtouchevent basis onintercepttouchevent ()   return value for processing Span style= "font-family: Arial" > return ture, Touch event handling return false do distribution event handling

View does not have a dispatchtouchevent () function , which is handled according to the callback of the Click event or the callback of the touch event .

Note 2: (Procedure for finding child controls )

The procedure for finding child controls is consistent with the event distribution process.

Infers whether the finger falls within the parent viewgroup rectangular area. The assumption is to traverse a child node and see it fall within that area of the child node. Until the child view is found .

Note 3 :

The result returned by the event dispatchtouchevent down pass is not trueby default, but rather a procedure.

Is the process of finding child controls.

dispatchtouchevent returns false by default during the process result-up pass .

Final: Overall process analysis

1. The Android event distribution is delivered to viewgroupfirst. Then passed by viewgroup to the View.

2, in the down pass process. It is possible to intercept event passing through Onintercepttouchevent and, if returned ture , no longer passes down, handling events directly from the control.

3.

2. in viewgroup , event passing can be intercepted by means of the onintercepttouchevent method, The Onintercepttouchevent method returns true for the non-consent event to continue passing to the child View , returning False represents an incorrect event for interception. False is returned by default .

3, when the event processing process is distributed upward,

Ontouch event for Button

returns ture. Do not respond to the click Event

returns false, responding to the click Event

depending on the underlying code, whether it returns ture or false, only this method is clickable,dispatchtouchevent will return true;

The dispatchtouchevent processing passed to viewGroup1 , assuming that its return value is set to false . the distribution event terminates . Handled by yourself (at which point the button and the viewGroup1 are returned to the Click event).

Suppose viewGroup1 's dispatchtouchevent returns to ture, which is to distribute upward, I do not deal with the meaning .

Only the handle logic of the button is in effect

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvamluznvsaw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">




Android event handling 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.