The following is an analysis of the source flow parsing the ListView initialization.
Layoutchildren () is called in Abslistview.onlayout and is implemented by the ListView
@Overrideprotected voidOnLayout (BooleanChangedintLintTintRintb) {Super. OnLayout (Changed, L, T, R, b); Minlayout=true; Final intChildCount =Getchildcount (); if(changed) { for(inti = 0; i < ChildCount; i++) {Getchildat (i). Forcelayout (); } mrecycler.markchildrendirty (); } //Subclass ImplementationLayoutchildren (); Minlayout=false; Moverscrollmax= (B-T)/Overscroll_limit_divisor; //Todo:move somewhere sane. This doesn ' t belong in OnLayout (). if(Mfastscroll! =NULL) {mfastscroll.onitemcountchanged (Getchildcount (), mitemcount); } }
Listview.layoutchildren:
@Overrideprotected voidLayoutchildren () {Final BooleanBlocklayoutrequests =mblocklayoutrequests; if(blocklayoutrequests) {return; } mblocklayoutrequests=true; Try { Super. Layoutchildren (); Invalidate (); if(Madapter = =NULL) {resetlist (); Invokeonitemscrolllistener (); return; } Final intChildrentop =Mlistpadding.top; Final intChildrenbottom = Mbottom-mtop-Mlistpadding.bottom; Final intChildCount =Getchildcount (); .. .....default: if(ChildCount = = 0) { if(!Mstackfrombottom) { Final intPosition = lookforselectableposition (0,true); Setselectedpositionint (position); //initialization will be performed hereSEL =fillfromtop (childrentop); } Else { Final intPosition = Lookforselectableposition (mItemCount-1,false); Setselectedpositionint (position); Sel= FillUp (mItemCount-1, Childrenbottom); } } Else { if(mselectedposition >= 0 && Mselectedposition <Mitemcount) {sel=fillspecific (mselectedposition, Oldsel==NULL?ChildrenTop:oldSel.getTop ()); } Else if(Mfirstposition <Mitemcount) {sel=fillspecific (mfirstposition, Oldfirst==NULL?ChildrenTop:oldFirst.getTop ()); } Else{sel= fillspecific (0, Childrentop); } } Break; }
Listview.fillfromtop:
Private View fillfromtop (int nexttop) { = math.min (mfirstposition, mselectedposition); = Math.min (mfirstposition, mItemCount-1); if (Mfirstposition < 0) { = 0; } return FillDown (mfirstposition, nexttop); }
Listview.filldown:
PrivateView FillDown (intPosintnexttop) {View Selectedview=NULL;
Height of the ListViewintEnd = (Mbottom-mtop); if((Mgroupflags & clip_to_padding_mask) = =clip_to_padding_mask) {End-=Mlistpadding.bottom; } //when initializing pos = 0, if the total item count is less than one screen, execute mitemcount-pos times//If Item is extra a screen, perform end-nexttop times while(Nexttop < end && Pos <Mitemcount) { //Is this the selected item? BooleanSelected = pos = =mselectedposition; View Child= Makeandaddview (pos, Nexttop,true, Mlistpadding.left, selected); Nexttop= Child.getbottom () +Mdividerheight; if(selected) {Selectedview=Child ; } POS++; } setvisiblerangehint (Mfirstposition, Mfirstposition+ Getchildcount ()-1); returnSelectedview; }
Android ListView Initialization will instantiate how many item