RecyclerView in Android Studio – Part 1

What is RecyclerView?

RecyclerView is advanced version of ListView. It is designed to work with large scale of data with better performance and memory management. Combination of CardView and RecyclerView will give you more flexibility for designing complex views.

RecyclerView v/s ListView

Feature RecyclerView ListView
ViewHolder pattern Implicit. Compulsory to implement Explicit. No require to compulsory implement
Animation Large amount of animations Little amount of animations
Performance Comparatively high Comparatively less
Multipurpose Yes. You can use RecyclerView for ListView, GridView and Staggered View No. You require to use different view like GridView
Memory Usage Comparatively less Comparatively high

Let’s start implementing ‘RecyclerView’ in project step by step

Step 1

Create a new project from File -> New project and fill all necessary details and select ‘Blank Activity’.

Step 2

Android Studio has introduce a new way to integrate dependency integration in ‘Gradle’. Hence for using ‘RecyclerView’ in your project, require to add the following ‘gradle’ dependency to project ‘build.gradle’ file.



Open build.gradle file and add below libraries in ‘dependency’ tag.

This will add all necessary library files in our project and give a dependency on that. It will download binaries from server of provided version ‘23.1.1’. Other libraries will be helpful for image display and downloading.

Step 3

You have done with dependency integration. Now require to add RecyclerView in your layout.

When you create application with ‘Blank Activity’, it will create and activity_main.xml layout file. Open layout file and add view inside.

Notice that in the above code snippet, a ‘RecyclerView’ user required following things.
– Take reference of RecyclerView’ from ‘support v7’ library and declare it
– Give ‘id’ to declared ‘RecyclerView’
– Set ‘height’ and ‘width’ of in it
– Set ‘scrollbars’ to vertical mode.

Step 4

Two main things require, while you are working with ‘ListView’ or ‘RecyclerView’.

1. Entity ( to bind your View)
2. Item layout file ( to design your item)

Step 4.1

What we want is list of some personalities and their image. Create a class file called and define necessary parameters in it.

Step 4.2

Create a new layout file called item_layout.xml ( design item layout for our list)

In above layout we took parent as CardView. This will show our each list item as card. When we see in design, it will look like

RecyclerView Item layout

RecyclerView Item layout

Step 5

In RecyclerView, it is mandatory to define ViewHolder. Hence to create it, create a new class file called ItemHolder.

Holder class are those which are defined with layout views used in items. In our case it is only single TextView.

Here, we require to understand functionality and importance of RecyclerView.Holder.

If you noticed, we declared a class and we are creating sub class of RecyclerView.ViewHolder by extending it.

As we created sub class, we require to pass our newly created object to super class by calling super(view). This will pass our view object to parent class.

Rest thing are like we are doing for finding view and assigning to widget.

This will search for id from provided view object and convert it to view object and assigns to provided object “txtCelebName”

Step 6

All we did is just preparation for things get ready to use in Adapter. But actual use will be in next Adapter which we are going to define.

Create class by extending RecyclerView.Adapter like below.

6.1 Class declaration

When you write this like, application will create a sub class of ‘RecyclerView.Adapter<>‘. Here, you require to pass ‘ViewHolder’ you have created previously in Step – 5

6.2 Constructor

You require to create a constructor to pass your list data so that adapter can work on it. In this example, we are passing list of “Celebrity” class which we created in Step 4. Simply create a file ‘celebrityList’ in adapter and assign list which is passed in constructor.

6.3 ViewHolder mapping

This is override method of Adapter class. It has main functionality of binding view with Holder. So in this method we need to create view by inflating layout and pass that newly created view object in our ItemHolder constructor. Rest of work will be taken care by our ‘ItemHolder’ class. (See Step 5)

6.4 Binding Holder with item

As described in 6.3, we created ItemHolder which will be returned in this method as argument. So we need to give data to bind them. As you can see in this method, we are taking item from list and setting text to ‘txtCelebName’ & ‘txtCelebMovie’ objects. Then after we gave reference of URL to Picasso library and displayed downloaded image to ‘profileImage’ object.

6.5. Counting list item

This method will tell adapter size of item list. Whatever count you are passing here, adapter will create items for RecyclerView and push them to it. Make sure you are passing proper size of list as in previous step you are taking object from that list. If you pass wrong argument, you will encounter an exception for IndexOutOfBoundsException.

Step 7

This is final step to assign adapter to RecyclerView. So open your file and paste below code.

We first need to first take view from layout and assign it to object. Then we will create an object of ItemAdapter with dummy data. We already prepared dummy data with help of ‘fillDummyData()’ method.

Step 8

Run your application. Output will be like below.

Download Code

Source code for this article is available here.

You may also like to read

Develop apps faster using Data Binding – Par... Google has been pushing updates to existing tools and making the life of android developers easier. They also have been releasing new libraries and AP...
Develop apps faster using Data Binding – Par... Google has been pushing updates to existing tools and making the life of android developers easier. They also have been releasing new libraries and AP...
Auto Restart application after Crash/Force Close i... In an Android application, we usually got the "Force Closed" error if we didn't get the exceptions right. Everyone has question about "How can I resta...
Read Logs programmatically in Android In this blog you will learn how to read logs programmatically in Android and used for bug report purposes. Its easy to read logs from Logcat an...
Share on Facebook1Share on Google+1Tweet about this on TwitterShare on LinkedIn0Share on Reddit0Pin on Pinterest0Share on Tumblr0Email this to someone