Recycleview Simple usage

Source: Internet
Author: User

Effect:

Mdrvactivity:
 Public classMdrvactivityextendsappcompatactivity {String imgur2= "Http://images.rednet.cn/ArticleImage/2017/04/25/201716483007449.jpg"; String Imgurl3= "Http://images.rednet.cn/ArticleImage/2017/04/25/2017167258494983.jpg"; String Geyan= "Education represents the past, ability represents the present, learning power represents the future"; PrivateRecyclerview Mrecyclerview; PrivateRecyclerview.adapter Madapter; PrivateRecyclerview.layoutmanager Mlayoutmanager; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.LAYOUT.RECYCLE_MDRV);        InitData ();    Initview (); }    Private voidInitData () {Mlayoutmanager=NewLinearlayoutmanager ( This, Linearlayoutmanager.vertical,false); Madapter=NewMyadapter (GetData ()); }    Private voidInitview () {//Recyclerview control ID, getting the Recyclerview controlMrecyclerview =(Recyclerview) Findviewbyid (R.id.my_recycler_view); //setting up the layout managerMrecyclerview.setlayoutmanager (Mlayoutmanager); //Set AdapterMrecyclerview.setadapter (Madapter); Mrecyclerview.additemdecoration (NewMydivideritemdecoration ( This, linearlayoutmanager.vertical)); }  /*private arraylist<newslistdata> GetData () {arraylist<newslistdata> data = new ARRAYLIST<NEWSL        Istdata> ();            for (int i = 0; i < i++) {newslistdata newslistdata=new newslistdata (imgur2,geyan+i, "10-11 recommended");        Data.add (Newslistdata);    } return data; }*/  protectedList GetData () {list Mdatas=NewArraylist<string>();  for(inti = ' A '; i < ' z '; i++) {Mdatas.add ("" + (Char) (i); }      returnMdatas; }}
Myadapter:
 Public classMyadapterextendsRecyclerview.adapter<myadapter.viewholder>{    PrivateList<string>Mdata;  Publicmyadapter (List data) { This. Mdata =data; }     Public voidUpdateData (List data) { This. Mdata =data;    Notifydatasetchanged (); } @Override PublicViewholder Oncreateviewholder (viewgroup parent,intViewType) {        //materialized view of the displayView v = layoutinflater.from (Parent.getcontext ()). Inflate (r.layout.my, parent,false); //Instantiate ViewholderViewholder Viewholder =NewViewholder (v); returnViewholder; } @Override Public voidOnbindviewholder (Viewholder holder,intposition) {        //binding DataHolder.mTv.setText ((charsequence) mdata.get (position)); } @Override Public intGetItemCount () {returnMdata = =NULL? 0: Mdata.size (); }     Public Static classViewholderextendsRecyclerview.viewholder {TextView mTv;  PublicViewholder (View itemview) {Super(Itemview); MTv=(TextView) Itemview.findviewbyid (R.ID.ITEM_TV); }    }}
Mydivideritemdecoration Add split Line:
 Public classMydivideritemdecorationextendsrecyclerview.itemdecoration {Private Static Final int[] Attrs =New int[]{android.    R.attr.listdivider};  Public Static Final intHorizontal_list =Linearlayoutmanager.horizontal;  Public Static Final intVertical_list =linearlayoutmanager.vertical; /*** For drawing interval styles*/    Privatedrawable Mdivider; /*** The direction of the list, horizontal/Vertical*/    Private intmorientation;  PublicMydivideritemdecoration (Context context,intorientation) {        //Get properties for a default theme        FinalTypedArray A =context.obtainstyledattributes (attrs); Mdivider= a.getdrawable (0);        A.recycle ();    SetOrientation (orientation); } @Override Public voidOnDraw (Canvas C, Recyclerview parent, recyclerview.state state) {//Draw Interval        if(Morientation = =vertical_list)        {Drawvertical (c, parent); } Else{Drawhorizontal (c, parent); }} @Override Public voidgetitemoffsets (Rect outrect, view view, Recyclerview parent, recyclerview.state state) {if(Morientation = =vertical_list) {Outrect.set (0, 0, 0, Mdivider.getintrinsicheight ()); } Else{Outrect.set (0, 0, mdivider.getintrinsicwidth (), 0); }    }    Private voidSetOrientation (intorientation) {        if(Orientation! = Horizontal_list && Orientation! =vertical_list) {            Throw NewIllegalArgumentException ("Invalid orientation"); } morientation=orientation; }    /*** Draw Interval*/    Private voiddrawvertical (Canvas C, Recyclerview parent) {Final intleft =Parent.getpaddingleft (); Final intright = Parent.getwidth ()-parent.getpaddingright (); Final intChildCount =Parent.getchildcount ();  for(inti = 0; i < ChildCount; i++) {            FinalView Child =Parent.getchildat (i); FinalRecyclerview.layoutparams params =(recyclerview.layoutparams) child. Getlayoutparams (); Final inttop = Child.getbottom () + Params.bottommargin +Math.Round (Viewcompat.gettranslationy (child)); Final intBottom = top +mdivider.getintrinsicheight ();            Mdivider.setbounds (left, top, right, bottom);        Mdivider.draw (c); }    }    /*** Draw Interval*/    Private voiddrawhorizontal (Canvas C, Recyclerview parent) {Final inttop =Parent.getpaddingtop (); Final intBottom = Parent.getheight ()-Parent.getpaddingbottom (); Final intChildCount =Parent.getchildcount ();  for(inti = 0; i < ChildCount; i++) {            FinalView Child =Parent.getchildat (i); FinalRecyclerview.layoutparams params =(recyclerview.layoutparams) child. Getlayoutparams (); Final intleft = Child.getright () + Params.rightmargin +Math.Round (Viewcompat.gettranslationx (child)); Final intright = left +mdivider.getintrinsicheight ();            Mdivider.setbounds (left, top, right, bottom);        Mdivider.draw (c); }    }}

Recycle_main.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    xmlns:tools=" Http://schemas.android.com/tools "    android:orientation=" Vertical "    android:layout_width=" Match_parent "    android:layout_height=" 500px "     Android:layout_margintop= "100px"    tools:ignore= "Missingdefaultresource" >    <  Android.support.v7.widget.RecyclerView        Android:id= "@+id/my_recycler_view"        android: ScrollBars= "vertical"        android:layout_width= "Match_parent"        android:layout_ Height= "Match_parent"/></linearlayout>

Item.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout   xmlns:android= "http://schemas.android.com/apk/ Res/android "    xmlns:tools=" Http://schemas.android.com/tools "    android:orientation=" Vertical "android:layout_width=" Match_parent "android:layout_height=" 50DP "    tools: Ignore= "Missingdefaultresource" ><TextView    android:id= "@+id/item_tv"    Android : Layout_width= "Match_parent"    android:layout_height= "Match_parent"    android: Gravity= "center"    /></linearlayout>

Recycleview Simple usage

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.