Previously used in the development of the ListView, now all the project into Recycleview, the following is the use of recycleview beginning to use some of the experience:
1, the Linearlayoutmanager must be initialized with Recycleview development: Recyclerview.setlayoutmanager (Linearlayoutmanager);
2, 3 methods must be implemented in adapter to inherit Recyclerview.adapter<recyclerview.viewholder>,adapter when using Recycleview
① Oncreateviewholder initializing the layout to be loaded
② onbindviewholder to load data
③ GetItemCount Get the size of the data source
3, and the same as the ListView using Recycelview nested in the scrollview inside the time there will be some problems, recycleview nested in the ScrollView will not show the data
At this point, we need to rewrite Linearlayoutmanager as follows:
Public classFulllinearlayourmanagerextendsLinearlayoutmanager {Private Static FinalString TAG = Com.mfyg.unified.customviews.FullLinearLayourManager.class. Getsimplename (); PublicFulllinearlayourmanager (Context context) {Super(context); } PublicFulllinearlayourmanager (Context context,intOrientationBooleanreverselayout) { Super(context, orientation, reverselayout); } Private int[] Mmeasureddimension =New int[2]; @Override Public voidonmeasure (Recyclerview.recycler recycler, recyclerview.state State,intWidthspec,intHeightspec) { Final intWidthmode =View.MeasureSpec.getMode (WIDTHSPEC); Final intHeightmode =View.MeasureSpec.getMode (HEIGHTSPEC); Final intWidthsize =View.MeasureSpec.getSize (WIDTHSPEC); Final intHeightsize =View.MeasureSpec.getSize (HEIGHTSPEC); LOG.I (TAG,"Onmeasure called. \nwidthmode "+Widthmode+ "\nheightmode" +Heightspec+ "\nwidthsize" +widthsize+ "\nheightsize" +heightsize+ "\ngetitemcount ()" +GetItemCount ()); intwidth = 0; intHeight = 0; for(inti = 0; I < GetItemCount (); i++{measurescrapchild (recycler, I, View.MeasureSpec.makeMeasureSpec (i, View.measurespec). UNSPECIFIED), View.MeasureSpec.makeMeasureSpec (i, View.MeasureSpec.UNSPECIFIED), mMe Asureddimension); if(getorientation () = =Horizontal) {Width= width + mmeasureddimension[0]; if(i = = 0) {Height= Mmeasureddimension[1]; } } Else{Height= height + mmeasureddimension[1]; if(i = = 0) {width= Mmeasureddimension[0]; } } } Switch(widthmode) { CaseView.MeasureSpec.EXACTLY:width=widthsize; CaseView.MeasureSpec.AT_MOST: CaseView.MeasureSpec.UNSPECIFIED:}Switch(heightmode) { CaseView.MeasureSpec.EXACTLY:height=heightsize; CaseView.MeasureSpec.AT_MOST: CaseView.MeasureSpec.UNSPECIFIED:} setmeasureddimension (width, height); } Private voidMeasurescrapchild (Recyclerview.recycler recycler,intPositionintWidthspec,intHeightspec,int[] measureddimension) { Try{View View= recycler.getviewforposition (0);//fix dynamic add times indexoutofboundsexception if(View! =NULL) {Recyclerview.layoutparams P=(Recyclerview.layoutparams) view.getlayoutparams (); intChildwidthspec =Viewgroup.getchildmeasurespec (Widthspec, Getpaddingleft ()+getpaddingright (), p.width); intChildheightspec =Viewgroup.getchildmeasurespec (Heightspec, Getpaddingtop ()+Getpaddingbottom (), p.height); View.measure (Childwidthspec, Childheightspec); measureddimension[0] = view.getmeasuredwidth () + P.leftmargin +P.rightmargin; measureddimension[1] = View.getmeasuredheight () + P.bottommargin +P.topmargin; Recycler.recycleview (view); } } Catch(Exception e) {e.printstacktrace (); } finally { } }}
Recycleview's experience "1"