Recycleview's experience "1"

Source: Internet
Author: User

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"

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.