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.

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 that extends RecyclerView.ViewHolder.

Now, write a class say that extends RecyclerView.Adapter

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.


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


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


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


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.


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

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

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

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

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

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

Share & Like

Tutorial topics

About 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.