Android:viewgroup Event Distribution mechanism

Source: Internet
Author: User

There have been many articles on the ViewGroup event distribution mechanism, recommending two articles from Guo Lin and Yang,
http://blog.csdn.net/guolin_blog/article/details/9153747
http://blog.csdn.net/lmj623565791/article/details/39102591
In combination with what they write, they simply summarize themselves and may only apply to individuals.

Process

As mentioned in the previous article, when you manipulate a control, whether it is pressed, moved, lifted, or not consumed in the middle of the event, the system will follow View.dispatchTouchEvent -> OnTouchListener.onTouch -> View.onTouchEvent the distribution. In fact, the event is first distributed to, ViewGroup dispatchTouchEvent and then distributed to onInterceptTouchEvent , the method, and the second method, by its name, is used to intercept consumption events. The code below, the default is not to intercept.

publicbooleanonInterceptTouchEvent(MotionEvent ev) {      returnfalse;  }  

Next look at the ViewGroup.dispatchTouchEvent method, the source is relatively long in order not to occupy space, you can directly in the above article to see. Observation source can be found that when the 按下 control event is distributed to the onInterceptTouchEvent method, and the default method is to return false, and then the system through the calculation of coordinates to determine whether the currently pressed control is a sub-view inside the viewgroup, if so, the event is distributed to the child view, That is, according to the View.dispatchTouchEvent -> OnTouchListener.onTouch -> View.onTouchEvent * *, and assign this sub-view to the target record, to facilitate the movement of the back control, lifting the recognition of the operation. If not, you will have the following code

ifnull) {          ev.setLocation(xf, yf);          if0) {              ev.setAction(MotionEvent.ACTION_CANCEL);              mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;          }          returnsuper.dispatchTouchEvent(ev);      }  

You can see that if the click is not a sub-view, then the event will be distributed to the super.dispatchTouchEvent method, and ViewGroup's parent class is the view, so you can understand, click on a control, first in the ViewGruop.dispatchTouchEvent inside to determine whether the click is a child view, is distributed according to the event distribution mechanism of view, otherwise the viewgroup itself as a view is distributed according to the event distribution mechanism of view. And the common viewgroup is nothing more than some layout and adapterview and so on.

Summary
    • If ViewGroup can find a child view, the event is distributed to the child view, otherwise it is a view distribution event.
    • By overriding the ViewGroup.onInterceptTouchEvent method, we can intercept the events of different actions and handle the events by ViewGroup itself.

Analysis of two articles, we generally do not need to rewrite ViewGroup and view dispatchTouchEvent methods and View.onTouchEvent methods, the programmer should be concerned about ViewGroup.onInterceptTouchEvent and onTouch , the former is to implement ViewGroup sub-view event interception, the latter is to implement the view distribution process onTouchmethods to onTouchEvent intercept the methods, but to onTouchEvent achieve onLongClick onClick the logic of judgment and other actions.
ViewGroup.onInterceptTouchEventBy default false , when you click to a child view, the event is distributed to the child view, returned or absent, and the onTouch event is false OnTouchListener distributed onTouchEvent , onLongClick returned, or absent false OnLongClickListener . Events are only possible to pass to onClick methods

Android:viewgroup Event Distribution 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.