Android RecyclerView Tutorial

Android RecyclerView widget

RecyclerView Implementation

Hey Technoz, This RecyclerView implementation tutorial comes with the solution to the dynamic listing of elements. The android RecyclerView is an advanced version of ListView in android. When we want to display some large list of items, then RecyclerView is a best option. It creates and binds some view holders on either side of the list. As soon as the user scrolls the list, the RecyclerView creates new view holders as necessary. It also saves the view holders which have scrolled off-screen, so they can be reused. The RecyclerView widget reuses old view holders with the new updated data. Thus, it reduces the memory consumption and improves the app’s performance. Of course, you can read more about RecyclerView widget on the Android Develper’s Site.

In our android recyclerview implementation project, we are showing a list of products with their price and description. So, lets start.

Add the gradle dependency

At first, to add RecyclerView widget, we need to add its gradle dependency in app-level build.gradle as follows. It is mandatory step in RecyclerView implementation.

Create the activity with RecyclerView

Now create a new empty activity. We will place our RecyclerView widget in this activity. Lets design the xml file first. In my case, it is activity_products.xml

Lets see the java file as below. It is

In above file, we have created an array of products list and added some items in it. We have here defined Adapter to bind data and LinearLayoutManager to fill the views.

Create class for getting and setting values

To get and set values of TextViews in our views, we are creating a new class with getter and setter methods.

So, we are here assigning the values to the strings as name, price and description which are sent to this class. Also, we can get back these values when required using getter methods.

Design the repeating view

The RecyclerView duplicates the same view dynamically with different values in it. So, we have to create the layout of single view which gets repeatedly generated as required. For recyclerview implementation, create the file product_list_row.xml as follows.

Add an Adapter

In order to feed the data to the list, we create and Adapter class. We extend this class to RecyclerView.Adapter class. This object creates views for items, and replaces the content of some of the views with new data items when the original item is no longer visible. Create the following java file

We have created a class MyViewHolder which contains all the views we require to fill the values in. The method OnCreateViewHolder() inflates the layout file product_list_row.xml. Similarly, the next method onBindViewHolder() gets the values from and it set the values according to the position of item.

Finally, we are done with the project. Now lets try running what we have made. You will see the RecyclerView implementation, listing 5 items in the array. Please see the screenshot below.

Android RecyclerView Tutorial

Please note that the products listed above does not necessarily belongs to the actual prices, descriptions and other data. It is only for demonstration purpose. The name of brands are the property of their respective owners.

Download Source Code

Especially, if you want to jump directly to the RecyclerView implementation, the source code is always available to download from below link.

If you have any doubts or problems in implementing this tutorial, please comment below. Please subscribe to our newsletter to catch such knowledgeable and understanding tutorials.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.