Android RecyclerView


Download Android RecyclerView sample

Android The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views.

In this tutorial we are going to discuss following topics:

  1. Where we should use RecyclerView
  2. Benefit of Android RecyclerView over ListView
  3. How to use custom layout in RecyclerView
  4. Intercept user touch events
  5. Customization of RecyclerView appearance
  6. How to handle click event of RecyclerView item

Where use Android RecyclerView?

We should use RecyclerView widget when we have data collections whose elements change at runtime based on user action or network events.

What is benefit of using Android RecyclerView?


RecyclerView introduces an additional level of abstraction between the RecyclerView.Adapter and RecyclerView.LayoutManager to be able to detect data set changes in batches during a layout calculation. This saves LayoutManager from tracking adapter changes to calculate animations. It also helps with performance because all view bindings happen at the same time and unnecessary bindings are avoided.

How to add RecyclerView support library?


To use the RecyclerView widget, we must add these 2 libraries. The first one is appcompat-v7, which ensures that the material styles are backward compatible and second one is the Design Support Library which will allows us to use RecyclerView.

Open your build.gradle of modeule app and add support library dependency, something like below given code.

Customer is my model class, which I am going to use for demonstration.

Customer.java

How to customize rows in Android RecyclerView?

To customize Android RecyclerView we need to do 2 things:

  1. We must specify a row layout, how we want to display rows in Android RecyclerView
  2. We must override onCreateViewHolder & onBindViewHolder method in our adapter class
I created a customer_row.xml under res ->layout, code of customer_row.xml is given below:

Here is screenshot, how above layout looks like:

Android ListView row

Implementing custom adapter for Android RecyclerView?


To implement Custom adapter for Android RecyclerView, first we need to Write a ViewHolder class say MyViewHolder.java that extends RecyclerView.ViewHolder.

MyViewHolder.java

Now, write a class say CustomerAdapter.java that extends RecyclerView.Adapter

CustomerAdapter.java

How to handle item click event of a RecyclerView?


Handling click event of Android RecyclerView is not as easy as it is with Android ListView.

To handle click event of Android RecyclerView create a class say RecyclerViewOnItemClickListener and implement RecyclerView.OnItemTouchListener.

Now add the onItemTouchListener for recyclerView as given below:

Android RecyclerView example


In this example, we are going to demonstrate how to use Android RecyclerView with Image and Text.

Result

Android RecyclerView

Step 1: Create a new project in Android Studio and name it Android RecyclerView.

Step 2: Open res -> layout -> activity_main.xml and add following code

activity_main.xml

Step 3: Open res -> values -> strings.xml and replace it with below given code.

strings.xml

Step 4: Open your res ->values ->colors.xml and replace it's code with below given code of my colors.xml as follows:

colors.xml

Step 5: Create a new layout resource under res -> layout named customer_row.xml. Replace the code of customer_row.xml with below given code.

customer_row.xml

Step 6: Create a class Customer.java and replace it's code with below given code of my Customer.java as follows:

Customer.java

Step 7:Create a class MyViewHolder.java and replace it's code with below given code of my MyViewHolder.java as follows:

MyViewHolder.java

Step 8:Create a class CustomerAdapter.java and replace it's code with below given code of my CustomerAdapter.java as follows:

CustomerAdapter.java

Step 9:Create a class RecyclerViewOnItemClickListener.java and replace it's code with below given code of my RecyclerViewOnItemClickListener.java as follows:

RecyclerViewOnItemClickListener.java

Step 10: Open your MainActivity.java and replace it's code with below given code of my MainActivity.java as follows:

MainActivity.java

Step 11: Clean and run the project to see the result.


Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.