Android RecyclerView Example

Android RecyclerView Example

The Android RecyclerView is a enhanced and flexible version of List View. RecyclerView is a container for displaying large amount of data. In this tutorial we are going to learn how to use a simple RecyclerView with a custom row layout.
Basically android RecyclerView needs an adapter to bind data and adapter needs a view holder to hold a view of each data item of data set.

Android RecyclerView Adapter

Adapters help RecyclerView to bind data from app-specific data set using views. There are several public methods in adapters which we have to implement inside android RecyclerView adapter.

  • getItemCount : Returns the total number of items in the data set held by the adapter.
  • onBindViewHolder : Called by RecyclerView to display the data at the specified position.
  • onCreateViewHolder : Called when RecyclerView needs a new view holder.

ViewHolder

Android RecyclerView uses view holder to define a view row per position. Generally view holder holds the layout of item view in RecyclerView.

ArrayList

In order to display data inside android RecyclerView, first of all we must have data stored in something. ArrayList is used to store multiple data with insertion order maintained. In this tutorial we will use an ArrayList of custom object to store data.

Model Class

Model class used to hold the data of a single data item. We can define all the properties of object in model class, for example if we take move as object then it’s properties will be movie name, release date, poster, genre etc. We need to define constructor or getter and setter methods in order to store and get data from this model class.


Such information is enough to jump into the tutorial.

Create a new android project in android studio and prepare for tutorial. First of all we will create essential layouts.

activity_main.xml

 

quote_item_container.xml

So our layouts are ready, now let’s start with model class and RecyclerView adapter class.

Quote.java

QuotesAdapter.java


MainActivity.java

Here are some other resources.

colors.xml

Here we go, following is the output of our tutorial.

Android RecyclerView

More about Android RecyclerView: Recycler View | Android Developers


Leave a Reply

Your email address will not be published. Required fields are marked *