Event distribution mechanism for view

Source: Internet
Author: User

One, click the event delivery rules

the primary method for passing events :

public boolean dispatchtouchevent (motionevent ev)

If the event can be passed to the current view, the method must be called.

Return value: Indicates whether the view receives the event, and the result is affected by the Dispatchtouchevent () method of the current ontouchevent and downlevel view.

public boolean onintercepttouchevent (motionevent ev)

Return value: Indicates whether to intercept the current event.

Note: When the method receives the down event, no other method of the same sequence of events will call the method (after the source code analysis)

The same sequence of events: a process of a series of actions that a finger taps, slides, and moves away

public boolean ontouchevent (motionevent ev)

Role: Used to handle click events

There are three related events in ViewGroup: Onintercepttouchevent, Dispatchtouchevent, Ontouchevent.

There are only two related events for view: Dispatchtouchevent, Ontouchevent.

Three ways to relate to each other

 Public Booleandispatchtouchevent (motionevent ev) {Booleanconsume =false; //determine whether to intercept the event   if(onintercepttouchevent (EV)) {//call The view's Ontouchevent methodconsume =ontouchevent (EV); } Else{       //do not intercept, then distribute to Child viewconsume =child.dispatchtouchevent (EV); }   //returns whether the ViewGroup consumes the current event when the traversal is finished   returnconsume;} 

First analyze the ViewGroup process: first, there must be a structural model concept: ViewGroup and view constitute a tree structure, the top of the activity of the viewgroup, there are a number of viewgroup nodes, There are several ViewGroup nodes or view nodes under each node, and so on.

When a touch event (for example, a touch event) reaches the root node, the acitivty ViewGroup, it is issued sequentially, and the process is implemented by invoking the Dispatchtouchevent method of the Child view (ViewGroup). Implementing a similar (relationship between three methods), first call Onintercepttouchevent () (ViewGroup's Onintercepttouchevent () returns false by default, so unless the method is overridden to return true)

If true, the first is to determine whether to set Ontouchlistenter, if set to call the Ontouch method, if the Ontouch method returns True, Ontouchevent is not called, and consumes the event. The Onclicklistener is set in Ontouchevent. Therefore, the priority of the Click event is generated.

returns FALSE, is that viewgroup iterates through the child view it contains, invokes the Dispatchtouchevent method of each view, and when the child view is ViewGroup, The Dispatchtouchevent method of its internal view is resumed by calling Viwgroup's Dispatchtouchevent method. The order of the messages in the above example is this: ①-②-⑤-⑥-⑦-③-④. The Dispatchtouchevent method is only responsible for the distribution of the event, which has a Boolean return value, and when returned to true, the order is interrupted. In the above example, if ⑤ 's Dispatchtouchevent returns True, ⑥-⑦-③-④ will not receive this touch event.

Http://www.cnblogs.com/linjzong/p/4191891.html

Event distribution mechanism for view

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.