Layout
< Android.support.v7.widget.RecyclerView Android:id = "@+id/rv_department" android:layout_width= "Match_parent" android:layout_height= "Match_ Parent " android:clipchildren=" false " android:cliptopadding = "false" android:padding= "8DP"/>
Item layout
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"> <Android.support.v7.widget.CardViewAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:layout_centerinparent= "true"Android:layout_margin= "8DP"> <TextViewAndroid:id= "@+id/tv"Android:layout_width= "Match_parent"Android:layout_height= "36DP"Android:background= "@drawable/bg_white_to_gray"Android:foreground= "? Attr/selectableitembackground"android:gravity= "Center"/> </Android.support.v7.widget.CardView></Relativelayout>
Adapter
Public classDeptadapterextendsRecyclerview.adapter<deptadapter.viewholder>{ Publicstring[] Datas =NULL; PublicDeptadapter (string[] datas) { This. datas =datas; } @Override PublicViewholder Oncreateviewholder (ViewGroup viewgroup,intViewType) {View View= Layoutinflater.from (Viewgroup.getcontext ()). Inflate (R.layout.item_dept,viewgroup,false); Viewholder VH=Newviewholder (view); returnVH; } @Override Public voidOnbindviewholder (Viewholder Viewholder,intposition) {ViewHolder.mTextView.setText (datas[position]); } @Override Public intGetItemCount () {returndatas.length; } classViewholderextendsRecyclerview.viewholder {PrivateTextView Mtextview; PublicViewholder (view view) {Super(view); Mtextview=(TextView) View.findviewbyid (r.id.tv); } }}
Gridlayoutmanager Way
New Gridlayoutmanager (this, 4); Rv_department.setlayoutmanager (manager); Rv_department.setitemanimator (new defaultitemanimator ()); // Set Animation New Deptadapter (new string[]{"Dongrun", "Development department", "Technology"}); Rv_department.setadapter (madapter);
Getting Started with Android programming--recyclerview using