"Translator" Android Recyclerview CardView

Source: Internet
Author: User

Android L Latest Support pack launches two UI controls Recycleview and CardView. Recyclerview is a more advanced, more flexible ListView, which is a big step forward because the ListView is one of the most commonly used controls in the UI component. In addition, the CardView control is a completely new component. In this tutorial, you'll explain how to use the two controls and how to mix them, first to get a deeper look at Recyclerview.

Recyclerview

As said earlier, Recyclerview is a more flexible ListView, though it introduces some complex stuff. We all know how to use the ListView in the app, and if you want to improve the performance of the ListView, you can use a pattern called Viewholder . This pattern consists of a simple class that holds a reference to the UI components contained in each row in the ListView. This mode avoids looking at those UI components when the list is displayed. Although this model introduces this benefit, we can still implement the ListView without using this pattern. and Recyclerview forces us to use Viewholder mode to improve the performance of the system. To illustrate how to use Recyclerview, we can create a simple app to display a list of address book cards. The first thing to do is to create the main layout file, Recyclerview like a ListView, and we can use them in the same way.

<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"  xmlns:tools="Http://schemas.android.com/tools"  android:layout_width="Match_parent"  android:layout_height="Match_parent"  android:paddingleft="@dimen/activity_horizontal_margin"  android:paddingright="@dimen/activity_horizontal_margin"  android:paddingtop="@dimen/activity_vertical_margin"  android:paddingbottom="@dimen/activity_vertical_margin"  tools:context=". MyActivity ">  <android.support.v7.widget.recyclerview         android:id="@+id/cardlist"         android:layout_width="Match_parent"         android:layout_height="Match_parent"   />    </RelativeLayout>

You should have noticed the layout above, Recycleview is in the Android support library, so we're going to modify the bulid.gradle file to include that dependency.

dependencies {   ...       Compile ' com.android.support:recyclerview-v7:21.0.0-rc1 '}

We can now onCreate get a reference to the Recycleview in the method and configure it.

@Overrideprotected void OnCreate (Bundle savedinstancestate) {      super.oncreate (savedinstancestate);      Setcontentview (r.layout.activity_my);      Recyclerview reclist = (recyclerview) Findviewbyid (r.id.cardlist);      Reclist.sethasfixedsize (true);      Linearlayoutmanager LLM = new Linearlayoutmanager (this);      Llm.setorientation (linearlayoutmanager.vertical);      Reclist.setlayoutmanager (LLM);}

You'll notice the difference between the Reclerview and the ListView, Recycleview needs a layout manager , which puts the list item view inside the line to decide when to loop the view. This library provides a default layout manager called LinearLayoutManager .

CardView

The CardView UI component displays more information in the card. You can customize its rounded corners, shadows, and so on. Now use this component to display communication information. The card will act as a recyclerview line, and later we can see how to integrate the two components and now define the layout of the card.

<android.support.v7.widget.cardview xmlns:card_view= "Http://schemas.android.com/apk/res-auto" xmlns:android= "  Http://schemas.android.com/apk/res/android "android:id=" @+id/card_view "android:layout_width=" Match_parent " android:layout_height= "Match_parent" card_view:cardcornerradius= "4DP" android:layout_margin= "5DP" >< Relativelayout android:layout_width= "match_parent" android:layout_height= "Match_parent" > <TextView android: Id= "@+id/title" android:layout_width= "match_parent" android:layout_height= "20DP" android:background= "@color/BK     G_card "android:text=" Contact det "android:gravity=" center_vertical "android:textcolor=" @android: Color/white " Android:textsize= "14DP"/><textview android:id= "@+id/txtname" android:layout_width= "Wrap_content" Androi d:layout_height= "Wrap_content" android:text= "Name" android:gravity= "center_vertical" android:textsize= "10DP" a ndroid:layout_below= "@id/title" android:layout_margintop= "10DP "android:layout_marginleft=" 5DP "/><textview android:id=" @+id/txtsurname "android:layout_width=" Wrap_co Ntent "android:layout_height=" wrap_content "android:text=" Surname "android:gravity=" center_vertical "android:t Extsize= "10DP" android:layout_below= "@id/txtname" android:layout_margintop= "10DP" android:layout_marginleft= "5DP" /><textview android:id= "@+id/txtemail" android:layout_width= "wrap_content" android:layout_height= "Wrap_cont Ent "android:text=" Email "android:textsize=" 10DP "android:layout_margintop=" 10DP "Android:layout_alignparentri Ght= "true" android:layout_marginright= "150DP" android:layout_alignbaseline= "@id/txtname"/></relativelayout >

As you can see, CardView is very simple to use, this component is in another support library, now to add dependencies:

dependencies {    compile ' com.android.support:cardview-v7:21.0.0-rc1 '    compile ' com.android.support: Recyclerview-v7:21.0.0-rc1 '}
Recyclerview:adapter

The adapter component provides data information that the UI component renders, in other words, an adapter tells the UI what information is displayed. So if we want to display the communication information, we need to give Recyclerview an adapter. The adapter must inherit RecyclerView.Adapter . Pass the Myholder class to implement Viewholder mode.

public class Myadapter extends Recyclerview.adapter<myholder> {...}

Now we need to cover two ways to implement our logic. is called when a new Viewholder instance is created, called when so onCreateViewHolder onBindViewHolder attempts to bind the data, in other words, when the data is displayed in the UI.

In this case, the adapter helped us combine recyclerview and CardView, and the card layout we defined previously would be the row for the Recyclerview address list. Before we do this, we need to define a data model (for example, what information needs to be displayed), and for this purpose we can define a simple class:

public class ContactInfo {  protected String name;  protected String surname;  protected String Email;  Protected static final String Name_prefix = "name_";  Protected static final String Surname_prefix = "Surname_";  Protected static final String Email_prefix = "Email_";}

Finally, prepare to create the adapter, and if you remember the viewholder pattern that you said before, we need to write code to implement it.

public static class Contactviewholder extends Recyclerview.viewholder {protected TextView vname; protected TextView Vsurn Ame protected TextView Vemail; protected TextView Vtitle; Public Contactviewholder (View v) {      super (v);      VName =  (TextView) V.findviewbyid (r.id.txtname);      Vsurname = (TextView)  V.findviewbyid (r.id.txtsurname);      Vemail = (TextView)  V.findviewbyid (r.id.txtemail);      Vtitle = (TextView) V.findviewbyid (r.id.title);  }}

As seen from the code, in the constructor of the class, we get a reference to the attempt to define the card layout, and now write the adapter code:

public class Contactadapter extends recyclerview.adapter<contactadapter.contactviewholder> {private list<    Contactinfo> contactlist;    Public Contactadapter (list<contactinfo> contactlist) {this.contactlist = ContactList;    } @Override public int getitemcount () {return contactlist.size (); } @Override public void Onbindviewholder (Contactviewholder contactviewholder, int i) {ContactInfo ci = conta        Ctlist.get (i);        ContactViewHolder.vName.setText (Ci.name);        ContactViewHolder.vSurname.setText (Ci.surname);        ContactViewHolder.vEmail.setText (Ci.email);   ContactViewHolder.vTitle.setText (Ci.name + "" + ci.surname); } @Override Public Contactviewholder oncreateviewholder (viewgroup viewgroup, int i) {View Itemview = Layoutinf                    Later.                    From (Viewgroup.getcontext ()).        Inflate (R.layout.card_layout, ViewGroup, false);   return new Contactviewholder (Itemview);public static class Contactviewholder extends Recyclerview.viewholder {...}} 

In the code implementation, bind the data to the attempt when we overwrite it onBindViewHolder . Note that we're not going to find the UI component simply refers to the information stored in Ccontactviewholder. The Oncreateviewholder returns the row for the Contactviewholder fill layout (in this example, CardView).

Run the app and you'll see the following results:

"Translator" Android Recyclerview CardView

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.