The use of the CardView of Android5.0

Source: Internet
Author: User

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?
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
  3. xmlns:app="Http://schemas.android.com/apk/res-auto"
  4. Android:layout_width="Match_parent"
  5. android:layout_height="Match_parent"
  6. android:orientation="vertical" >
  7. <android.support.v7.widget.cardview
  8. Android:layout_width="Match_parent"
  9. android:layout_height="108DP"
  10. app:cardcornerradius="10DP"
  11. app:cardelevation="5DP" >
  12. <relativelayout
  13. Android:layout_width="Match_parent"
  14. android:layout_height="Match_parent" >
  15. <imageview
  16. android:id="@+id/iv"
  17. Android:layout_width="108DP"
  18. android:layout_height="108DP"
  19. android:padding="12DP"
  20. Android:scaletype="Centercrop"/>
  21. <textview
  22. android:id="@+id/content"
  23. Android:layout_width="Match_parent"
  24. android:layout_height="Wrap_content"
  25. Android:layout_centervertical="true"
  26. android:layout_marginleft="36DP"
  27. android:layout_torightof="@id/iv"
  28. android:gravity="center"
  29. android:padding="3DP"
  30. android:text=""/>
  31. </RelativeLayout>
  32. </android.support.v7.widget.CardView>
  33. </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?
  1. Public class Myadapter extends recyclerview.adapter<recyclerview.viewholder> {
  2. private list<itementity> List;
  3. private context context;
  4. private Layoutinflater Inflater;
  5. Public Myadapter (context context, list<itementity> List) {
  6. This.context = context;
  7. this.list = list;
  8. Inflater = Layoutinflater.from (context);
  9. }
  10. @Override
  11. Public Recyclerview.viewholder Oncreateviewholder (viewgroup parent, int viewtype) {
  12. View view = Inflater.inflate (R.layout.item, null);
  13. return new Myviewholder (view);
  14. }
  15. @Override
  16. public void Onbindviewholder (Recyclerview.viewholder holder, int position) {
  17. Myviewholder Holder1 = (myviewholder) holder;
  18. Itementity itementity = list.get (position);
  19. Holder1.content.setText (Itementity.getcontent ());
  20. Holder1.imageView.setImageResource (Itementity.getimg ());
  21. }
  22. @Override
  23. public int GetItemCount () {
  24. return list.size ();
  25. }
  26. private class Myviewholder extends recyclerview.viewholder{
  27. private ImageView ImageView;
  28. private TextView content;
  29. Public Myviewholder (View itemview) {
  30. super (Itemview);
  31. ImageView = (ImageView) Itemview.findviewbyid (R.ID.IV);
  32. Content = (TextView) Itemview.findviewbyid (r.id.content);
  33. }
  34. }
  35. }


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?
  1. Public class Mainactivity extends Appcompatactivity {
  2. private list<itementity> List;
  3. @Override
  4. protected void OnCreate (Bundle savedinstancestate) {
  5. super.oncreate (savedinstancestate);
  6. Setcontentview (R.layout.activity_main);
  7. Recyclerview Recyclerview = (recyclerview) Findviewbyid (R.id.recycler_view);
  8. InitData ();
  9. Myadapter adapter = New Myadapter (this, list);
  10. Recyclerview.setlayoutmanager (new Linearlayoutmanager (this));
  11. Recyclerview.setadapter (adapter);
  12. }
  13. private void InitData () {
  14. List = new arraylist<> ();
  15. int[] IMGs = new int[]{r.drawable.p1,r.drawable.p2,r.drawable.p3,r.drawable.p4,r.drawable.p5, R.DRAWABLE.P6,
  16. R.DRAWABLE.P7,R.DRAWABLE.P8,R.DRAWABLE.P9,R.DRAWABLE.P10,R.DRAWABLE.P11,R.DRAWABLE.P12,R.DRAWABLE.P13,
  17. R.DRAWABLE.P14,R.DRAWABLE.P15};
  18. For (int img:imgs) {
  19. Itementity itementity = new Itementity ();
  20. Itementity.setcontent ("scenery" + img);
  21. Itementity.setimg (IMG);
  22. List.add (itementity);
  23. }
  24. }
  25. }


It's that simple.

Above.

The use of the CardView of Android5.0

Related Article

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.