Working With RecyclerView And CardView In Android

RecyclerView is one of the most important Views in Android development. Its name itself is a definition. It recycles a View. And, what is that view? It’s a card. The card holds the data of the user, and the RecyclerView displays it.

Think of your visiting card. It carries your information. Now, you make a stamp and it acts like a Recycler and prints your information on card.

The relation of RecyclerView and CardView in Android is the same. Let us look at the crucial steps required to build your best View application.

  1. RecyclerView.
  2. CardView.
  3. Adapter.
  4. Holder.
  5. Data (mostly in list form).
  6. Send data to Adapter class and assign that adapter to RecyclerView.

Step 1 - Make a RecyclerView

In activity.xml, write the following code.

  1. <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view_doctor_schedual" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" />// This is just a sample script. Paste your real code (javascript or HTML) here. if ('this_is'==/an_example/){of_beautifier();}else{var a=b?(c%d):e[f];}  
In Java back-end file, declare these global variables.
  1. RecyclerView mSchedualRecyclerView;  
  2. private static RecyclerView.Adapter adapter;  
  3. private RecyclerView.LayoutManager layoutManager;  
And, write this private function. Remember to call it in onCreate.
  1. private void initRecyclerView() {  
  2.     mSchedualRecyclerView = (RecyclerView) findViewById(R.id.recycler_view_doctor_schedual);  
  3.     mSchedualRecyclerView.setHasFixedSize(true);  
  4.     mSchedualRecyclerView.setItemAnimator(new DefaultItemAnimator());  
  5.     layoutManager = new LinearLayoutManager(this);  
  6.     mSchedualRecyclerView.setLayoutManager(layoutManager);  
  7. }  
So far, we have added a RecyclerView and initialized it. Now, we will make a card that will take data.

Step 2 Create a Card

Add a layout resource file and name it. I will recommend to start the name with card prefix so that you can find it easily in any project.



Now, add this code in your resource file.
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">  
  2.     <android.support.v7.widget.CardView android:id="@+id/card_view" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" card_view:cardBackgroundColor="@android:color/transparent" card_view:cardElevation="0dp">  
  3.         <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">  
  4.             <TextView android:id="@+id/text_view_name" android:layout_width="match_parent" android:layout_height="wrap_content" />  
  5.             <TextView android:id="@+id/text_view_address" android:layout_width="match_parent" android:layout_height="wrap_content" />  
  6.         </LinearLayout>  
  7.     </android.support.v7.widget.CardView>  
  8. </LinearLayout>  
Remember

Always, make heights as “wrap_content,” otherwise it will create UI problems. At this point, your RecyclerView and Card both are ready. Now, we need the data and an adapter.

Step 3 Create an Adapter

You can think of adapter as a switchboard base that will hold all the switches i.e list of customer names and addresses.

Make a model class.
  1. public class Customer {  
  2.     public static String mCustomerName;  
  3.     public static String mCustomerAddress;  
  4. }  
So far, everything has been simple. Now, a little tricky portion comes in, i.e. making an adapter. But, don’t worry! Just follow the following steps.

Make a class
  1. public class CustomerAdapter {}  
Step 4 Make a ViewHolder Class inside Adapter Class

Create a class inside CustomerAdapter, that will extend RecyclerView.ViewHolder.

Now, your code will look like this.
  1. public class CustomerAdapter {  
  2.     public class CustomerViewHolder extends RecyclerView.ViewHolder {  
  3.         public CustomerViewHolder(View itemView) {  
  4.             super(itemView);  
  5.         }  
  6.     }  
  7. }  
Now, extend the main class with extends RecyclerView.Adapter<CustomerAdapter.CustomerViewHolder>

And then, implement the methods. Now, your final adapter code will look like this.
  1. public class CustomerAdapter extends RecyclerView.Adapter < CustomerAdapter.CustomerViewHolder > {  
  2.     @Override  
  3.     public CustomerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
  4.         return null;  
  5.     }  
  6.     @Override  
  7.     public void onBindViewHolder(CustomerViewHolder holder, int position) {}  
  8.     @Override  
  9.     public int getItemCount() {  
  10.         return 0;  
  11.     }  
  12.     public class CustomerViewHolder extends RecyclerView.ViewHolder {  
  13.   
  14.         public CustomerViewHolder(View itemView) {  
  15.             super(itemView);  
  16.         }  
  17.     }  
  18. }  
Now, your adapter is ready. So, you need to tell your adapter which card it will use to show the data.

Make the following changes in the above onCreate View Holder Override function.
  1. @Override  
  2. public CustomerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
  3.     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout_schedual, parent, false);  
  4.     return new CustomerViewHolder(view);  
  5. }  
The adapter's main function is to take a list of customers to bind with the card. Then, the RecyclerView will show that card to the user. For this purpose, add the following in CustomerAdapter class.
  1. List < Customer > mCustomerList;  
  2. public CustomerAdapter(List < Customer > customerList) {  
  3. this.mCustomerList = customerList;  
  4. }  
The above constructor will provide a list of customers and in order to show data, now your final adapter will look like this.
  1. public class CustomerAdapter extends RecyclerView.Adapter < CustomerAdapter.CustomerViewHolder > {  
  2.     List < Customer > mCustomerList;  
  3.     public CustomerAdapter(List < Customer > customerList) {  
  4.         this.mCustomerList = customerList;  
  5.     }  
  6.     @Override  
  7.     public CustomerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
  8.         View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_customer, parent, false);  
  9.         return new CustomerViewHolder(view);  
  10.     }  
  11.     @Override  
  12.     public void onBindViewHolder(CustomerViewHolder holder, int position) {  
  13.         Customer customer = mCustomerList.get(position);  
  14.         holder.tvName.setText(customer.mCustomerName);  
  15.         holder.tvAddress.setText(customer.mCustomerAddress);  
  16.     }  
  17.     @Override  
  18.     public int getItemCount() {  
  19.         return mCustomerList.size();  
  20.     }  
  21.     public class CustomerViewHolder extends RecyclerView.ViewHolder {  
  22.         TextView tvName, tvAddress;  
  23.         public CustomerViewHolder(View itemView) {  
  24.             super(itemView);  
  25.             this.tvName = (TextView) itemView.findViewById(R.id.text_view_name);  
  26.             this.tvAddress = (TextView) itemView.findViewById(R.id.text_view_address);  
  27.         }  
  28.     }  
  29. }  
The CustomerViewHolder initializes the TextViews and on BindViewHolder set the text of TextViews from the data in the list.

Step 5 Get your Data

Now, go to your activity and add a method to add customers in list.
  1. private List < Customer > setmCustomerList() {  
  2.     mCustomerList = new ArrayList < Customer > ();  
  3.     Customer customer = new Customer();  
  4.     customer.mCustomerName = "First Customer Name";  
  5.     customer.mCustomerAddress = "First CustomerAddress";  
  6.     customer = new Customer();  
  7.     mCustomerList.add(customer);  
  8.     customer.mCustomerName = "2nd Customer Name";  
  9.     customer.mCustomerAddress = "2nd CustomerAddress";  
  10.     customer = new Customer();  
  11.     mCustomerList.add(customer);  
  12.     customer.mCustomerName = "3rd Customer Name";  
  13.     customer.mCustomerAddress = "3rd CustomerAddress";  
  14.     mCustomerList.add(customer);  
  15.     customer = new Customer();  
  16.     customer.mCustomerName = "4th Customer Name";  
  17.     customer.mCustomerAddress = "4th CustomerAddress";  
  18.     mCustomerList.add(customer);  
  19.     return mCustomerList;  
  20. }  
Step 6 Give Adapter to RecyclerView

Now, edit initRecyclerView(). It will look like this.
  1. private void initRecyclerView() {  
  2.     mSchedualRecyclerView = (RecyclerView) findViewById(R.id.recycler_view_doctor_schedual);  
  3.     mSchedualRecyclerView.setHasFixedSize(true);  
  4.     //setting animation  
  5.     mSchedualRecyclerView.setItemAnimator(new DefaultItemAnimator());  
  6.     layoutManager = new LinearLayoutManager(this);  
  7.     //binding layout with recycler view  
  8.     mSchedualRecyclerView.setLayoutManager(layoutManager);  
  9.     //assigning adapter to RecyclerView  
  10.     adapter = new CustomerAdapter(setmCustomerList());  
  11.     mSchedualRecyclerView.setAdapter(adapter);  
  12. }  
Now, you are good to go.

 

Up Next
    Ebook Download
    View all
    Learn
    View all