This control is used to display a large number of datasets in a limited window, in fact, the control of such a function is not unfamiliar to us, for example: ListView, GridView
By setting the different layoutmanager,itemdecoration it provides, itemanimator achieves an astonishing effect.
- The way you want to control its display, through the layout manager LayoutManager
- You want to control the interval between item (can be drawn), please pass the itemdecoration
- You want to control the animation of item additions and deletions, please pass Itemanimator
- You want to control the click, long press events, please write yourself
Activity_main.xml
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <Android.support.v7.widget.RecyclerViewAndroid:id= "@+id/id_recyclerview_horizontal"Android:layout_width= "Match_parent"Android:layout_height= "120DP"android:layout_centervertical= "true"Android:scrollbars= "None" /></Relativelayout>
Item.xml
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "120DP"Android:layout_height= "120DP"> <ImageViewAndroid:id= "@+id/iv"Android:layout_width= "80DP"Android:layout_height= "80DP"Android:layout_alignparenttop= "true"Android:layout_centerhorizontal= "true"Android:layout_margin= "5DP"Android:scaletype= "Centercrop"/> <TextViewAndroid:id= "@+id/tv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@id/iv"Android:layout_centerhorizontal= "true"Android:layout_margintop= "5DP"Android:layout_marginbottom= "5DP"Android:text= "some info"android:textsize= "12DP" /></Relativelayout>
Mainactivity.java
Public classMainactivityextendsappcompatactivity {PrivateRecyclerview MRV; PrivateList<integer>Mdatas; PrivateGalleryadapter Madapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); InitData (); MRV=(Recyclerview) Findviewbyid (r.id.id_recyclerview_horizontal); Linearlayoutmanager Li=NewLinearlayoutmanager ( This); Li.setorientation (linearlayoutmanager.horizontal); Mrv.setlayoutmanager (li);//Set Layout manager Madapter=NewGalleryadapter ( This, Mdatas); Mrv.setadapter (Madapter);//set Adapter } Private voidInitData () {Mdatas=NewArraylist<integer>(Arrays.aslist (R.drawable.a, r.drawable.b, r.drawable.c, R.DRAWABLE.E, R.DRAWABLE.F, R.D RAWABLE.G)); }}
galleryadapter. java
Public classGalleryadapterextends recyclerview.adapter<galleryadapter.viewholder> { PrivateLayoutinflater Minflter; PrivateList<integer>Mdatas; PublicGalleryadapter (context context, list<integer>datas) {Minflter=Layoutinflater.from (context); Mdatas=datas; } /*** The Viewholder we create must inherit the Recyclerview.viewholder, which must be passed into a view when constructed, this view is equivalent to our ListView g Convertview in Etview * (that is, we need to inflate the item layout to be passed in). */ Public classViewholderextends recyclerview.viewholder{ PublicViewholder (View v) {Super(v); } ImageView Miv; TextView MTV; } //Create Viewholer@Override PublicViewholder Oncreateviewholder (viewgroup parent,intViewType) {View View= Minflter.inflate (R.layout.item,parent,false); Viewholder Viewholder=Newviewholder (view); VIEWHOLDER.MIV=(ImageView) View.findviewbyid (R.ID.IV); returnViewholder; } //set the value; Bind data to Viewholder@Override Public voidOnbindviewholder (Viewholder holder,intposition) {Holder.miv.setImageResource (Mdatas.get (position)); } //How many data are there?@Override Public intGetItemCount () {returnmdatas.size (); }}
Android Development Learning--recycleview Getting Started