:
*recycler_view_item.xml
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Android:layout_width= "Match_parent"Android:layout_height= "108DP"android:orientation= "Vertical" > <Android.support.v7.widget.CardView Android:id= "@+id/card_view"Android:layout_width= "Match_parent"Android:layout_height= "108DP"App:cardcornerradius= "5DP"app:cardelevation= "5DP" > <relativelayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <ImageView Android:id= "@+id/iv"Android:layout_width= "128DP"Android:layout_height= "96DP"android:layout_centervertical= "true"android:padding= "12DP"Android:scaletype= "Centercrop"android:src= "@drawable/p1"/> <TextView Android:id= "@+id/username"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"android:layout_centervertical= "true"Android:paddingright= "36DP"Android:text= "username"/> </RelativeLayout> </android.support.v7.widget.CardView></LinearLayout>
*activity_main.xml
<android.support.design.widget.CoordinatorLayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "Org.mobiletrain.a8_2coordinatorlayoutrecyclerview." Mainactivity "> <android.support.design.widget.AppBarLayout android:layout_width= "Match_parent"Android:layout_height= "200DP" > <android.support.design.widget.CollapsingToolbarLayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent"App:contentscrim= "@color/colorprimary"App:layout_scrollflags= "Scroll|enteralwayscollapsed|exituntilcollapsed|snap"App:title= "Coordinator Layout" > <ImageView android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:scaletype= "Centercrop"android:src= "@drawable/p1"App:layout_collapsemode= "Parallax"App:layout_collapseparallaxmultiplier= "0.7"/> <Android.support.v7.widget.Toolbar Android:id= "@+id/tool_bar"Android:layout_width= "Match_parent"Android:layout_height= "? Attr/actionbarsize"App:layout_collapsemode= "Pin" ></android.support.v7.widget.Toolbar> </android.support.design.widget.collapsingtoolbarlayout > </android.support.design.widget.AppBarLayout> <Android.support.v7.widget.RecyclerView Android:id= "@+id/recycler_view"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"App:layout_behavior= "@string/appbar_scrolling_view_behavior" ></android.support.v7.widget.recyclerview></ Android.support.design.widget.coordinatorlayout>
*myadapter
Public classMyadapterextendsRecyclerview.adapter<recyclerview.viewholder> { Privatecontext Context; PrivateLayoutinflater Inflater; PrivateList<string>list; Private int[] colors; Privaterandom random; PublicMyadapter (context context, list<string>list) { This. Context =context; This. List =list; Random=NewRandom (); Colors=New int[]{color.red, Color.green, Color.Blue, color.yellow}; Inflater=Layoutinflater.from (context); } @Override PublicRecyclerview.viewholder Oncreateviewholder (viewgroup parent,intViewType) {View View= Inflater.inflate (R.layout.recycler_view_item,NULL); return Newmyviewholder (view); } @Override Public voidOnbindviewholder (Recyclerview.viewholder holder,intposition) {(myviewholder) holder). Username.settext (List.get (position)); //set the background color for CardView((myviewholder) holder). Cardview.setcardbackgroundcolor (Colors[random.nextint (4)]); } @Override Public intGetItemCount () {returnlist.size (); } classMyviewholderextendsRecyclerview.viewholder {PrivateTextView username; PrivateCardView CardView; PublicMyviewholder (View itemview) {Super(Itemview); Username=(TextView) Itemview.findviewbyid (r.id.username); CardView=(CardView) Itemview.findviewbyid (R.id.card_view); } }}
*mainactivity
Public classMainactivityextendsappcompatactivity {PrivateList<string>list; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Toolbar Toolbar=(Toolbar) Findviewbyid (R.id.tool_bar); Setsupportactionbar (toolbar); Recyclerview Recyclerview=(Recyclerview) Findviewbyid (R.id.recycler_view); InitData (); Recyclerview.setlayoutmanager (NewLinearlayoutmanager ( This)); Myadapter Adapter=NewMyadapter ( This, list); Recyclerview.setadapter (adapter); } Private voidInitData () {list=NewArraylist<>(); for(inti = 0; I < 30; i++) {List.add ("Lisi:" +i); } }}
"Frame" Coordinatorlayout&recyclerview Coordinator layout + card layout