ViewGroup lifecycle execution steps for Android custom controls

Source: Internet
Author: User

Preface
It is important to understand how the ViewGroup life cycle is performed in order to customize ViewGroup, so that the entire process can have a deeper understanding of viewgroup. This article from a personal summary, to explain the order of execution.

Execution notes

First, the most common life cycles for ViewGroup are: Construction method, OnLayout (), Onfinishinflate (), Onmeasure (), Onsizechanged (), the first two must be rewritten when creating ViewGroup subclasses. As for draw () and drawchild () are used for drawing backgrounds and sub-view, it is not in the life cycle of one by one narration.

The first: directly referenced in XML, the order of execution is generally: Construction method->onfinishinflate () (only once)->onmeasure () (can be executed more than once)->onsizechanged () ( This method is recalled when the onmeasure is not the same as the size measured earlier)->onlayout () (Layout sub-view)->onmeasure ()->onlayout ().
 
The second type: Setcontentview (Newcustomview (this)) in activity, the order of execution is compared to the first, except for the inconsistent and non-execution onfinishinflate of the constructed method reference ( ) Outside, the other is basically the same.
 
The third type: The constructor is only executed when the activity is directly new CustomView (this) and no parent layout is added, others will not execute.

Summary tip:

Onmeasure () generally defines the measurement dimensions and the height of the child controls. The
first sets the size of the ViewGroup itself as follows:

int widthsize = measurespec.getsize (Widthmeasurespec);  int heightsize = measurespec.getsize (Heightmeasurespec); Setmeasureddimension (Widthsize, heightsize);

Then set the dimensions of the sub-view such as the following:

View leftmenuview = getchildat (1);  marginlayoutparams lp =  ( Marginlayoutparams)                    leftmenuview.getlayoutparams ();  final int drawerwidthspec =  Getchildmeasurespec (widthmeasurespec,                   mmindrawermargin + lp.leftmargin + lp.rightmargin,                    Lp.width);   final int drawerheightspec = getchildmeasurespec (HeightMeasureSpec,                    lp.topmargin + lp.bottommargin,                 &nbsP; lp.height);   leftmenuview.measure (Drawerwidthspec, drawerheightspec);     view contentview = getchildat (0);  lp =  (MarginLayoutParams)   Contentview.getlayoutparams ();  final int contentwidthspec =  Measurespec.makemeasurespec (                   widthSize - lp.leftMargin - lp.rightMargin,  measurespec.exactly);   final int contentheightspec = measurespec.makemeasurespec (                   heightsize - lp.topmargin - lp.bottommargin, measurespec.exactly);   Contentview.measure (CONTENTWIDTHSPEC, CONTENTHEIGHTSPEC);

the int getchildmeasurespec (int spec,int padding,int childdimension) returns the measurement size spec spec, which can be set padding for the sub view, You do not need to set padding directly as Contentview.
OnLayout () Layout sub-view location, basically use layout (left,top,right,bottom);

GetWidth () and Getmeasurewidth () the Difference and contact:
Getmeasuredwidth (): As soon as the Setmeasureddimension () method is executed, it has a value and no longer changes. In a nutshell, after executing the Onmeasure method, you can get it.
GetWidth () can only be obtained after executing onmeasure (), but it is possible that the layout sizing should change, and if OnLayout () does not modify the width height of the child view, then two values are equal.

Periodic Table of Life:

Reference: http://blog.csdn.net/anydrew/article/details/50985763

ViewGroup lifecycle execution steps for Android custom controls

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.