CardView is also a very cool control, generally we will cardview with Recyclerview to use, of course, CardView can also cooperate with the ListView to use, are can. OK, let's take a look at a Cardview+recyclerview implementation:
Each item is rounded, and there is a shadow effect, this is Google's MD design specifications, there is a three-dimensional feeling, rounded effect should be very easy to do, but the effect of the shadow if you want to do it really is not easy. Well, let's see how it works today!
1. Add dependencies
Here I use the recyclerview+cardview to achieve this effect, of course, if you use a ListView can achieve this effect. OK, I need the following two dependencies, one Recyclerview, one is CardView.
Compile ' com. android.support:recyclerview-v7:23.1.1 '
Compile ' com.android.support:cardview-v7:23.1.1 '
2. Constructing the adapter of Recyclerview
To construct the Recyclerview adapter, I'll first post the item's layout:
[Java]View Plaincopy print?
- <?xml version="1.0" encoding="Utf-8"?>
- <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="Match_parent"
- android:orientation="vertical" >
- <android.support.v7.widget.cardview
- Android:layout_width="Match_parent"
- android:layout_height="108DP"
- app:cardcornerradius="10DP"
- app:cardelevation="5DP" >
- <relativelayout
- Android:layout_width="Match_parent"
- android:layout_height="Match_parent" >
- <imageview
- android:id="@+id/iv"
- Android:layout_width="108DP"
- android:layout_height="108DP"
- android:padding="12DP"
- Android:scaletype="Centercrop"/>
- <textview
- android:id="@+id/content"
- Android:layout_width="Match_parent"
- android:layout_height="Wrap_content"
- Android:layout_centervertical="true"
- android:layout_marginleft="36DP"
- android:layout_torightof="@id/iv"
- android:gravity="center"
- android:padding="3DP"
- android:text=""/>
- </RelativeLayout>
- </android.support.v7.widget.CardView>
- </LinearLayout>
As you can see, I used the CardView node in the layout of item to indicate that an item is a card, where the app:cardcornerradius= "10DP" property represents the fillet size of each item, app:cardelevation= " 5DP "Property represents altitude, is actually the height of the z axis, after setting the z-axis height also has the shadow effect, of course you can also set other properties, such as app:cardbackgroundcolor=" "to indicate the background color of each item. The rest is simple.
Look again at adapter:
[Java]View Plaincopy print?
- Public class Myadapter extends recyclerview.adapter<recyclerview.viewholder> {
- private list<itementity> List;
- private context context;
- private Layoutinflater Inflater;
- Public Myadapter (context context, list<itementity> List) {
- This.context = context;
- this.list = list;
- Inflater = Layoutinflater.from (context);
- }
- @Override
- Public Recyclerview.viewholder Oncreateviewholder (viewgroup parent, int viewtype) {
- View view = Inflater.inflate (R.layout.item, null);
- return new Myviewholder (view);
- }
- @Override
- public void Onbindviewholder (Recyclerview.viewholder holder, int position) {
- Myviewholder Holder1 = (myviewholder) holder;
- Itementity itementity = list.get (position);
- Holder1.content.setText (Itementity.getcontent ());
- Holder1.imageView.setImageResource (Itementity.getimg ());
- }
- @Override
- public int GetItemCount () {
- return list.size ();
- }
- private class Myviewholder extends recyclerview.viewholder{
- private ImageView ImageView;
- private TextView content;
- Public Myviewholder (View itemview) {
- super (Itemview);
- ImageView = (ImageView) Itemview.findviewbyid (R.ID.IV);
- Content = (TextView) Itemview.findviewbyid (r.id.content);
- }
- }
- }
Adapter is the ordinary Recyclerview adapter, which is very simple, nothing to say.
3. Pack up the Recyclerview and show
Finally, initialize the data in the activity and give it to the Recyclerview to display it:
[Java]View Plaincopy print?
- Public class Mainactivity extends Appcompatactivity {
- private list<itementity> List;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- Recyclerview Recyclerview = (recyclerview) Findviewbyid (R.id.recycler_view);
- InitData ();
- Myadapter adapter = New Myadapter (this, list);
- Recyclerview.setlayoutmanager (new Linearlayoutmanager (this));
- Recyclerview.setadapter (adapter);
- }
- private void InitData () {
- List = new arraylist<> ();
- int[] IMGs = new int[]{r.drawable.p1,r.drawable.p2,r.drawable.p3,r.drawable.p4,r.drawable.p5, R.DRAWABLE.P6,
- R.DRAWABLE.P7,R.DRAWABLE.P8,R.DRAWABLE.P9,R.DRAWABLE.P10,R.DRAWABLE.P11,R.DRAWABLE.P12,R.DRAWABLE.P13,
- R.DRAWABLE.P14,R.DRAWABLE.P15};
- For (int img:imgs) {
- Itementity itementity = new Itementity ();
- Itementity.setcontent ("scenery" + img);
- Itementity.setimg (IMG);
- List.add (itementity);
- }
- }
- }
It's that simple.
Above.
The use of the CardView of Android5.0