Android RecyclerView

Whenever you want to display a large number of items in a scrollable list, most items are not visible. For example, in a long list of words or many news headlines, list of products from the ecommerce website the user only sees a small number of list items at a time.

The RecyclerView class is a more advanced and flexible version of ListView. It is used for displaying large data sets that can be scrolled very efficiently to maintaining a limited number of views.

Use the RecyclerView widget when you need to display a large amount of scrollable data dynamically,means  the data is fetched dynamically  from the server at the runtime of the app or data collections whose elements change at runtime based on user action or network events.

RecyclerView components:


It is not important that  where the data comes from. You can create the data locally, or  get it from the  database or from the server dynamically on the device.

A RecyclerView:

It contains large amount of data in the scrolling view. means view the data by scrolling it. Exmaple to use it for ecommerce producrs, news feeds, blog posts etc.

Layout for one item of data;

In recycler view whatever data we are fetching is similar so that we are using same layout for all the items. so that one item view at a time created and filled with data. Example :ecommerce product list, employers data and students details etc.

A layout manager:

The layout manager handles the layout of user interface in a view. All view groups have layout managers. For example the LinearLayout, the Android system itself handles the layout for view. RecyclerView requires an explicit layout manager to manage the user interface in a view list items contained within it. This layout may be  vertical, horizontal, or a grid etc

 An adapter:

The adapter connects your data to the RecyclerView. It prepares the data and how will be displayed in a view holder.

The adapter updates the contents of the respective list item view when data changes are done in the RecyclerView.

the  Adapter class  extends RecyclerViewAdapter. The adapter class uses a ViewHolder to hold the views that contains each item in the RecyclerView, and to bind the data to be displayed into the views that displays to the user.

A view holder:

The view holder extends the ViewHolder class. It contains the view information for displaying one item from the item’s layout. A view holder used by the adapter to supply data, and extends RecyclerView.ViewHolder class.


Any displayable data can be shown in a RecyclerView. The data maybe any format Text ,Images, Icons etc. that will be present in the app or we are fetched dynamically  while running the application from database or from server.

Creating a  Recycler View:

Implementing a RecyclerView requires the following steps:

Add the RecyclerView dependency to the app’s app/build.gradle file first to make it available to the layout file and activity class.

Add the RecyclerView to the activity’s layout file.

Create a layout XML file for one item

Create any java  class and Extend RecyclerView.Adapter and implement  onCreateViewHolder and onBindViewHolder methods inside it.

Extend RecyclerView.ViewHolder to create a view holder for your item layout. Here we can implement  click behavior by overriding the onClick() method.

In your activity class, Inside onCreate method, create a RecyclerView and initialize it with the adapter and a  layout manager.

Add the dependency to app/build.gradle :

dependencies {
compile ''

Note: In latest vrsion of the android in dependencies the complie is replaced with the implement.

Adding a RecyclerView to your activity’s layout:

Add the RecyclerView in your activity’s layout file.


Create the layout for one item:

Create an XML resource file and specify the layout of one item. This will be used by the adapter to create the view holder and will be used for all the items.

<LinearLayout xmlns:android=""

style="@style/word_title" />


Create an adapter with a view holder:

Create a new Java class with the following signature:

public class Adapter extends RecyclerView.Adapter<MyWordListAdapter.MyWordViewHolder> {


In the constructor, get an inflater from the current contex, and your data.

public MyWordListAdapter(Context context, LinkedList<String> wordList) {
mInflater = LayoutInflater.from(context);
this.myWordList = wordList;

For this adapter, you have to implement 3 methods.

onCreateViewHolder() creates a view and returns it.

public MyWordViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
// Inflate an item view.
View myItemView = mInflater.inflate(R.layout.mywordlist_item, parent, false);
return new MyWordViewHolder(myItemView, this);

onBindViewHolder() associates the data with the view holder for a given position in the RecyclerView:

public void onBindViewHolder(MyWordViewHolder holder, int position) {
// Retrieve the data for that position
String myCurrent = myWordList.get(position);
// Add the data to the view

getItemCount() returns to number of data items available for displaying to the user:

public int getItemCount() {
return myWordList.size();

Creating the view holder class:

class MyWordViewHolder extends RecyclerView.ViewHolder {}

If we want to add onclick events to handle then we need to implement onClickListener() method inside  MyWordViewHolder class.

// Extend the signature of WordVewHolder to implement a click listener.

class MyWordViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {}

In its constructor, the view holder has to inflate its layout, associate with its adapter, and, if applicable, set a click listener.

public MyWordViewHolder(View itemView, MyWordListAdapter adapter) {
MywordItemView = (TextView) itemView.findViewById(;
this.mAdapter = adapter;

And, if you implementing onClickListener, you also have to implement onClick().

public void onClick(View v) {
wordItemView.setText ("Clicked! "+ wordItemView.getText());

Create the RecyclerView :

In the mainActivity class create the instance of RecyclerView. and then create Adapetr class instance by supplying the data that will be dispalyed to the user and then connect the Adapter class to the RecyclerView to display the data. As shown in below example code.

Get a handle to the RecyclerView.

myRecyclerView = (RecyclerView) findViewById(;

Create an adapter and supply the data to be displayed.

myAdapter = new MyWordListAdapter(this, mWordList);

Connect the adapter with the recycler view.


Give the recycler view a default layout manager.

myRecyclerView.setLayoutManager(new LinearLayoutManager(this));


RecyclerView is used to display large amount of that is fetched dynamically from the database or from the server or that will be available within the app.The fetched data will be displayed in the scrolable view to the user. The data format may be text, images,  or any files.

Congratulations You Have Learned About Recycler View 

Leave a Reply