Event distribution and working mechanism of view

Source: Internet
Author: User

How the View works
Viewroot and Decorview
The 1.ViewRoot corresponds to the Viewrootimpl class, which is the link between WindowManager and Decorview, and the three processes of view are accomplished through viewroot.
In 2.ActivityThread, when activity is created, Decorview is added to the window, the Viewrootimpl object is created, and the association is established.
The 3.View drawing process begins with the Viewroot Performtraversals method, through the three processes of measure, layout, and draw.

event Distribution , which is the process of distributing the motionevent event, is passed on to a specific view, and the process of delivery is the process of distribution.
Three methods:
dispatchtouchevent: Used for the distribution of events, if the event can be passed to the current view, then this method must be called , The return result is affected by the ontouchevent and view dispatchtouchevent methods of the current view, indicating whether the current event is consumed
onintercepttouchevent: Used to determine whether to intercept an event, if the current view intercepts an event, then in the same sequence of events, this method will not be called again , the results indicate whether to intercept the current event;
ontouchevent: Called in the Dispatchtouchevent method to handle the Click event, the return result indicates whether the current event is consumed, and if not consumed, the current view** cannot receive the event again in the same sequence of events.

public   Boolean  dispatchtouchevent  (motionevent ev) { Boolean  consume = false ; if  (Onintercepttouchevent (EV)) {consume = ontouchevent (EV);} else  {consume = child.dispatchtouchevent (EV);} return  consume;  

activity -to-event distribution process
Activity, Window, Decorview.
Windows is an abstract class that controls the appearance and behavior policies of top-level view, and Phonewindow is the only implementation of this class.
Decorview is the underlying container of the current interface, that is, the view set by Setcontentview is a child view of it.
The distribution process for top-level View -to-click events
ViewGroup, Dispatchtouchevent, onintercepttouchevent, OnTouch or ontouchevent
Top view is generally a viewgroup. After intercepting the event, if ViewGroup set the Montouchlistener, the Ontouch method in listener will block out ontouchevent. If Ontouchevent is set to Monclicklistener, the onclick in the listener is called. If ViewGroup is not intercepted, pass it to the child view until the entire event is distributed.

View if there is no Onintercepttouchevent method, once a click event is passed to him, he will handle it.

Event Distribution order (from top to bottom, then bottom to top)
When a click event is generated, it is passed as follows: Activity, Window, View. If the view's Ontouchevent returns false, its parent container ontouchevent will be called, and so on, will eventually be processed by the activity's ontouchevent.

View Workflow:
Onmeasure, OnLayout, OnDraw
Custom View:
1. Directly inheriting view or ViewGroup needs to handle the wrap_content itself. (onmeasure)
2.View to process padding in the OnDraw method, and ViewGroup to handle onlayout and margin in onmeasure and padding.
The Post method in 3.View can replace handler.
4. Stop the animation, thread, or recycle other resources in the view's Ondetachedfromwindow.
5. Swipe conflict handling. There are three ways to achieve this:
1) scroller Content Slide
2) Animation
3) Layoutparams

Event distribution and working mechanism of 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.