Android has introduced a new RecyclerView widget which is used to create lists with material design style. It helps to provide fast scrolling of data. Large amount of data can be displayed and managed easily with the help of Android RecyclerView. This tutorial presents a recyclerview example to display data elements in list.

Listview vs RecyclerView

Listview creates view items for each data item. Suppose your data set is 100 strings. You pass them to adapter to get 100 view items. This is the case with listview. You can refer to simple listview using adapter tutorial to know more.

RecyclerView creates limited number of view items and it reuse them. Minimal number of views will be created to display the data on the screen. Views becomes reusable with the help of RecycleView. So, it is the efficient way of implementing List like view.

How Android RecyclerView widget works?

RecyclerView is a widget which was introduced in API 21. With the help of support libraries we can use it on older versions as well. To create a list view using RecyclerView, you need to create its LayoutManager and an Adapter.

  1. LayoutManager defines how items will be arranged in the list. You can create a scrolling list which is either horizontal or vertical. It is also possible to display the items in normal as well as in a staggered grid. You can also choose to create a custom LayoutManager for you to display view items as per requirements.
  2. Adapter takes data sources as an input and provided the corresponding android view items. These view items are then displayed in the list. Limited number of view items will be created to fit the screen which provides faster scrolling.

Implementing Android RecyclerView

Step #1: Add compile dependency.

Make sure you add compile dependency of RecyclerView in build.gradle file. I am using Android studio to demonstrate android recyclerview example.

Step #2: Design main layout file.

Use to include RecyclerView inside your xml file. Give it an id, set its width and heigh to match parent. Below you can find the complete code of activity_main.xml file.

Step #3: Design an item for your list.

I am using simple textview. It will be displayed as a single item. Create a new file by the name of item.xml and include the code given below.

Step #4: Create an adapter for RecyclerView.

Our goal is to supply string array as a data source. Then RecyclerAdapter class will provide view items which will be re-used to display data items. We extend RecyclerView.Adapter to create our custom adapter for android recycler view. ViewHolder class provides view items for our list. onCreateViewHolder() method helps to inflate the layout element.

How to replace the content of view item?

Recyclerview helps to create re-usable view items. onBindViewHolder() method helps to replace the data from view items. This provide fast scrolling.

Screenshot of android recyclerview example

android recyclerview

List using recycler view

Step #4: Implementing MainActivity for your android application.

I am using string as a data source. You can display data in different styles by using LayoutManager. We have used LinearLayoutManager to scroll data in vertical style (similar to listview). Find below the source code of file.

What next?

Learn to add cards to your list. You can add list like view in your application by using android cardview widget.

This was all about implementing simple list like view using recyclerview in android. RecyclerView supports material design like style and you can refer the tutorial to learn more about material designs in android. Comment below for your suggestions or queries. Thank you for reading this beautiful tutorial.