[Android FrameWork 6.0 Source Learning] Analysis of AddView function of ViewGroup

Source: Internet
Author: User

The entire view of Android is assembled using the combined mode.

ViewGroup is quite with the roots, various layout is equivalent to the branches, each seed view, is equivalent to the leaves.

As for the view class. Let's just think of it as a seed. Ha ha!

ViewGroup belong to the root, can grow a lot of branches (inherit the custom layout) and the branches can grow a lot of leaves (Textview,imageview ... )

Good, gossip less, next step into the subject!

First, the operation of the view is defined in a interface called Viewmanager, there are two methods in the interface, which are removed and updated, this time the main analysis AddView

 Public InterfaceViewmanager {/*** Assign The passed layoutparams to the passed view and add the view to the window. * <p>throws {@linkAndroid.view.WindowManager.BadTokenException} for certain programming * errors, such as adding a second view         to a window without removing the first view. * <p>throws {@linkandroid.view.WindowManager.InvalidDisplayException} If the window is on a * secondary {@linkDisplay} and the specified display can ' t be found * (see {@linkandroid.app.Presentation}). *@paramView the view to is added to this window. * @paramparams the layoutparams to assign to view. */         Public voidAddView (view view, viewgroup.layoutparams params);  Public voidupdateviewlayout (view view, viewgroup.layoutparams params);  Public voidRemoveview (view view)}

AddView is implemented in ViewGroup, and then the code is posted for analysis

     Public void AddView (View child, Layoutparams params) {        -1, params);    }

This method calls its own AddView method, and passes a number 1, the 1 is the index of the view, the position to insert

     Public voidAddView (View Child,intindex, Layoutparams params) {        if(DBG) {System.out.println ( This+ "AddView"); }        if(Child = =NULL) {            Throw NewIllegalArgumentException ("Cannot add a null child view to a ViewGroup"); }        //Addviewinner () would call Child.requestlayout () when setting the new Layoutparams//Therefore, we call Requestlayout () on ourselves before.//Would be blocked at our levelrequestlayout (); Invalidate (true); Addviewinner (Child, index, params,false); }

This method first redraws the layout and then calls the Addviewinner (child, index, params, false); method to insert the view into the appropriate location

    Private voidAddviewinner (View Child,intindex, layoutparams params,Booleanpreventrequestlayout) {        if(Mtransition! =NULL) {            //Don ' t prevent other add transitions from completing, but cancel remove//Transitions to let them complete the process before we add to the containerMtransition.cancel (layouttransition.disappearing); }        if(Child.getparent ()! =NULL) {            Throw NewIllegalStateException ("The specified child already have a parent." + "You must call Removeview () on the Child ' s parent first. "); }        if(Mtransition! =NULL) {Mtransition.addchild ( This, child); }        if(!Checklayoutparams (params)) {params=generatelayoutparams (params); }        if(preventrequestlayout) {Child.mlayoutparams=params; } Else{child.setlayoutparams (params); }        if(Index < 0) {Index=Mchildrencount; }        //ViewGroup uses an array of view types to maintain the sub-view below, this method is to add the view to the location of the responseAddinarray (Child, index); //The parent container of the view to which the binding is inserted is the current group//Tell our children        if(preventrequestlayout) {child.assignparent ( This); } Else{child.mparent= This; }        if(Child.hasfocus ()) {Requestchildfocus (Child, Child.findfocus ()); } attachinfo ai=Mattachinfo; if(AI! =NULL&& (Mgroupflags & flag_prevent_dispatch_attached_to_window) = = 0) {            BooleanLastkeepon =Ai.mkeepscreenon; Ai.mkeepscreenon=false; Child.dispatchattachedtowindow (Mattachinfo, (Mviewflags&visibility_mask)); if(Ai.mkeepscreenon) {needglobalattributesupdate (true); } Ai.mkeepscreenon=Lastkeepon; }        if(child.islayoutdirectioninherited ()) {child.resetrtlproperties ();        } dispatchviewadded (child); if((Child.mviewflags & duplicate_parent_state) = =duplicate_parent_state) {Mgroupflags|=Flag_notify_children_on_drawable_state_change; }        if(Child.hastransientstate ()) {childhastransientstatechanged (Child,true); }        if(Child.getvisibility ()! =view.gone) {notifysubtreeaccessibilitystatechangedifneeded (); }        if(Mtransientindices! =NULL) {            Final intTransientcount =mtransientindices.size ();  for(inti = 0; i < Transientcount; ++i) {Final intOldindex =Mtransientindices.get (i); if(Index <=Oldindex) {Mtransientindices.set (i, Oldindex+ 1); }            }        }    }

This method first checks the layoutparams to see if the inserted view has a long width, and if not, generates a default Layoutparams

Then determine whether the index is less than 0, less than 0 is assigned to the number in the current container, representing the insertion to the last item

The Addinarray method is then called to insert the view into the current viewgroup, and when the insert is complete, bind the view to the parent container (the value of GetParent)

Followed by some focus, monitoring the distribution, we carefully analyzed the insertion method is good, is Addinarray

    Private voidAddinarray (View Child,intindex) {view[] Children=Mchildren; Final intCount =Mchildrencount; Final intSize =children.length; if(Index = =count) {            if(Size = =count) {Mchildren=NewView[size +Array_capacity_increment]; System.arraycopy (Children,0, Mchildren, 0, size); Children=Mchildren; } Children[mchildrencount++] =Child ; } Else if(Index <count) {            if(Size = =count) {Mchildren=NewView[size +Array_capacity_increment]; System.arraycopy (Children,0, Mchildren, 0, index); System.arraycopy (children, index, Mchildren, index+ 1, Count-index); Children=Mchildren; } Else{system.arraycopy (children, index, children, index+ 1, Count-index); } Children[index]=Child ; Mchildrencount++; if(Mlasttouchdownindex >=index) {Mlasttouchdownindex++; }        } Else {            Throw NewIndexoutofboundsexception ("index=" + index + "count=" +count); }    }

Mchildren is an array of view types in ViewGroup that contains all the sub-view under the group

Array_capacity_increment the value of this constant is 12.

First determine whether the location of the mchildren is sufficient, not enough to continue to expand the 12 positions, copy the source array contents into the new array, and then put this secondary view to the last

If index is smaller than count, it is an insert operation, it is also the first to determine whether the position is sufficient, not sufficient to expand and copy to index, and then the remaining copy to Index+1 to the end

This will empty the index position. The insert operation is completed

Once the insert is complete, the system will redraw the interface and you will be able to see the view you have inserted.

AddView This method is analyzed, there is any doubt can be pointed out, the same mistake. Common learning and common progress.

[Android FrameWork 6.0 Source Learning] Analysis of AddView function of ViewGroup

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.