Recyclerview (Alternative listview) Usage Introduction

Source: Internet
Author: User

Add the following code in the Build.gradle file

‘com.android.support:cardview-v7:21.0.3‘‘com.android.support:recyclerview-v7:21.0.3‘
case with the same item type

Layout file

<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"Android:layout_width="Match_parent"android:layout_height="Match_parent"Android:paddingbottom="@dimen/activity_vertical_margin"android:paddingtop="@dimen/activity_vertical_margin"> <android. Support. V7. Widgets. RecyclerviewAndroid:id="@+id/recycler_view"Android:layout_width="Match_parent"android:layout_height="Match_parent"Android:layout_centerhorizontal="true"Android:layout_centervertical="true"/></relativelayout>

Page code

 Public  class oneactivity extends Activity {    PrivateRecyclerview Mrecyclerview; @Overrideprotected voidOnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_one);        Mrecyclerview = (Recyclerview) Findviewbyid (R.id.recycler_view); Mrecyclerview.setlayoutmanager (NewLinearlayoutmanager ( This));//Here with a linear display similar to the ListView//Mrecyclerview.setlayoutmanager (new Gridlayoutmanager (this, 2));//Here is a linear Gongge display similar to Grid view//Mrecyclerview.setlayoutmanager (New Staggeredgridlayoutmanager (3, orientationhelper.vertical));//This shows the class with a linear Gongge Like a waterfall in the streamMrecyclerview.setadapter (NewRecyclerviewadapter ( This)); }//recyclerview Adapter     Public  class recyclerviewadapter extends recyclerview. Adapter<recyclerview. Viewholder> {        PrivateContext Mcontext;PrivateLayoutinflater Mlayoutinflater;PrivateString[] Mcityname; PublicRecyclerviewadapter (Context mcontext) { This. Mcontext = Mcontext;            Mlayoutinflater = Layoutinflater.from (Mcontext);        Mcityname = Mcontext.getresources (). Getstringarray (R.array.city_name); }//Create new Viewholder, called by LayoutManager@Override PublicRecyclerview.viewholder Oncreateviewholder (viewgroup parent,intViewType) {return NewRecyclerviewviewholder (Mlayoutinflater.inflate (R.layout.item_text, parent,false)); }//Actions to bind data to the interface@Override Public voidOnbindviewholder (Recyclerview.viewholder holder,intPosition) {((recyclerviewviewholder) holder). Mtextview.settext (Mcityname[position]); }//Get the number of data@Override Public intGetItemCount () {returnMcityname = =NULL?0: Mcityname.length; }//Custom Viewholder, holding all interface elements for each item         Public  class recyclerviewviewholder extends recyclerview. Viewholder {TextView Mtextview; PublicRecyclerviewviewholder (View itemview) {Super(Itemview);                Mtextview = (TextView) Itemview.findviewbyid (r.id.tv_city); Mtextview.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {toastutil.show (Mcontext, getPosition () +"");            }                }); }        }    }}

:

case with different item types

Set up the adapter code

//recyclerview Adapter Public  class multiplerecyclerviewadapter extends recyclerview. Adapter<recyclerview. Viewholder> {    PrivateContext Mcontext;PrivateLayoutinflater Mlayoutinflater; Public Static enumItem_type {item_type_image, item_type_text} PublicMultiplerecyclerviewadapter (Context mcontext) { This. Mcontext = Mcontext;    Mlayoutinflater = Layoutinflater.from (Mcontext); }//Create new Viewholder, called by LayoutManager@Override PublicRecyclerview.viewholder Oncreateviewholder (viewgroup parent,intViewType) {if(ViewType = = Item_type. Item_type_text.ordinal ()) {return NewTextviewviewholder (Mlayoutinflater.inflate (R.layout.item_text, parent,false)); }Else{return NewImageviewviewholder (Mlayoutinflater.inflate (r.layout.item_image, parent,false)); }    }//Actions to bind data to the interface@Override Public voidOnbindviewholder (Recyclerview.viewholder holder,intPosition) {}//Get the number of data@Override Public intGetItemCount () {return Ten; }//item Type@Override Public intGetitemviewtype (intPosition) {returnPosition%2==0? Item_type. Item_type_text.ordinal (): Item_type.    Item_type_image.ordinal (); }//itemholder Type one     Public  class textviewviewholder extends recyclerview. Viewholder {TextView Mtextview; PublicTextviewviewholder (View itemview) {Super(Itemview);        Mtextview = (TextView) Itemview.findviewbyid (r.id.tv_city); }    }//itemholder Type two     Public  class imageviewviewholder extends recyclerview. Viewholder {ImageView Mimageview; PublicImageviewviewholder (View itemview) {Super(Itemview);        Mimageview = (ImageView) Itemview.findviewbyid (r.id.iv_city); }    }}

:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Recyclerview (Alternative listview) Usage Introduction

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.