Recyclerview Android Example [Beginners]

Recyclerview Android in the advanced version of the listview. RecyclerView simplifies the display and handle of the large set of data.It’s coming with default animations.RecyclerView provides layout managers for positioning items.

Recyclerview Official document says,

If your app needs to display a scrolling list of elements based on large data sets (or data that frequently changes), you should use RecyclerView as described on this page.


Advantages of Android Recyclerview

1. ViewHolder Pattern

In a ListView, it was recommended to use the ViewHolder pattern but it was never a compulsion. In case of RecyclerView, this is mandatory using the RecyclerView.ViewHolder class. This is one of the major differences between the ListView and the RecyclerView.

It makes things a bit more complex in RecyclerView but a lot of problems that we faced in the ListView are solved efficiently.

2. LayoutManager

This is another massive enhancement brought to the RecyclerView. In a ListView, the only type of view available is the vertical ListView. There is no official way to even implement a horizontal ListView.

Now using a RecyclerView, we can have a

i) LinearLayoutManager – which supports both vertical and horizontal lists,

ii) StaggeredLayoutManager – which supports Pinterest like staggered lists,

iii) GridLayoutManager – which supports displaying grids as seen in Gallery apps.

And the best thing is that we can do all these dynamically as we want.

3. Item Animator

ListViews are lacking in support of good animations, but the RecyclerView brings a whole new dimension to it. Using the RecyclerView.ItemAnimator class, animating the views becomes so much easy and intuitive.

4.Item Decoration

In case of ListViews, dynamically decorating items like adding borders or dividers was never easy. But in case of RecyclerView, the RecyclerView.ItemDecorator class gives huge control to the developers but makes things a bit more time consuming and complex.


RecyclerView includes a new kind of adapter. It’s a similar approach to the ones you already used, but with some peculiarities, such as a required ViewHolder. You will have to override two main methods: one to inflate the view and its view holder, and another one to bind data to the view. The

good thing about this is that first method is called only when we really need to create a new view. No need to check if it’s being recycled.

Also, I have added another post on adding pagination for the recyclerview.

Done with the explanation. Let's create sample application with recyclerview android.

1. Recyclerview Android Dependency

For using RecyclerView in your project you need to add the recycler view support library to your project. Add the following gradle dependency to project build.gradle file.

dependencies {
    implementation "androidx.recyclerview:recyclerview:1.1.0"

2. Create layout with Recyclerview

Add android RecyclerView into activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""


3. Create Data Model

Create Model class to hold the data for the recyclerview. In my case, I am using model  class.

public class Items {

    private String name;
    private int price;

    Items(String mName,int mPrice){ = mName;
        this.price = mPrice;

    public int getPrice() {
        return price;

    public void setPrice(int price) {
        this.price = price;

    public String getName() {
        return name;

    public void setName(String name) { = name;


4. Prepare Adapter for the Recyclerview 

Recylerview adapter used to hold the view with data. For that, we need to design layout resource file item_row.xml first.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""


        android:id="@+id/tvPrice" />

The design will be looks like this,

The UI for the recyclerview adapter is ready. next, create the adapter for the recyclerview. Recyclerview adapter should extends the RecyclerView.Adapter<ViewHolder>.

Also, We Recyclerview needed ViewHolder to hold the view items.

public class RecyclerviewItemAdapter extends RecyclerView.Adapter<RecyclerviewItemAdapter.MyViewHolder> {

    private List<Items> itemsList;
    private ClickListener clickListener;

    RecyclerviewItemAdapter(List<Items> mItemList){
        this.itemsList = mItemList;

    public RecyclerviewItemAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row,parent,false);
        return new MyViewHolder(view);

    public void onBindViewHolder(RecyclerviewItemAdapter.MyViewHolder holder, final int position) {
        final Items item = itemsList.get(position);;

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

    class MyViewHolder extends RecyclerView.ViewHolder{

        public TextView name,price;
        private LinearLayout itemLayout;

        public MyViewHolder(View itemView) {
            name = itemView.findViewById(;
            price = itemView.findViewById(;
            itemLayout =  itemView.findViewById(;

5. Prepare data for recyclerview

Once the adapter created, we need to prepare data for the recyclerview. So that, we can set it into recyclervirew using adapter. in

private void prepareItems(){
        for(int i = 0; i < 50; i++) {
            Items items = new Items("Item"+i,20+i);

In most cases, We use REST API to fetch data from the server.  Check below link for retrofit android with recyclerview.

6. Set Recyclerview Adapter to Recyclerview

In previous, we created adapter and data for the recyclerview. Now, we need to set it into recyclerview.

recyclerviewItemAdapter = new RecyclerviewItemAdapter(itemsList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setItemAnimator(new DefaultItemAnimator());

Thats it. we created the recyclerview with data. Now you can able run this and see the data in the recyclerview.

7. Implementing click listener for recyclerview

To implement click listener, First we need to create interface for the adapter and the recyclerview android.

create and add the needed interface.

public interface ClickListener<T> {

    void onClick(View view, T data, int position);

In my case, I am having once interface for the recyclerview item click listener.

Next, in your RecyclerviewViewAdapter create method to get the clickListener instance from your MainActivity.

public void setOnItemClickListener(ClickListener clickListener) {
        this.clickListener = clickListener; 

in, Call this method with the ClickListener implementation.

recyclerviewItemAdapter.setOnItemClickListener(new ClickListener<Items>(){
            public void onClick(View view, Items data, int position) {
                Toast.makeText(getApplicationContext(),"Position = "+position+"\n Item = "+data.getName(),Toast.LENGTH_SHORT).show();


Done. we you can able to click and get the callback for the recyclerview clicks.

Download the source code for the example in github.


Thanks for the reading. In recent time, we are using recyclerview every day. So try to do the sample with the recyclerview and let me know your doubts in the comments.



22 Replies to “Recyclerview Android Example [Beginners]”

  1. This design is incredible! You most certainly know how to keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Excellent job. I really enjoyed what you had to say, and more than that, how you presented it. Too cool!|

  2. magnificent points altogether, you just gained a new reader. What may you suggest in regards to your put up that you just made some days in the past? Any positive?|

  3. Hey! I could have sworn I’ve been to this site before but after checking through some of the post I realized it’s new to me. Anyhow, I’m definitely delighted I found it and I’ll be bookmarking and checking back often!|

  4. This design is incredible! You obviously know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Fantastic job. I really enjoyed what you had to say, and more than that, how you presented it. Too cool!|

  5. I get pleasure from, result in I found just what I was taking a look for. You’ve ended my 4 day lengthy hunt! God Bless you man. Have a great day. Bye|

  6. My brother recommended I might like this website. He was once entirely right. This submit actually made my day. You can not believe just how so much time I had spent for this information! Thanks!|

  7. Hello there! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Nonetheless, I’m definitely happy I found it and I’ll be book-marking and checking back often!|

  8. If some one wishes to be updated with latest technologies afterward he must be go to see this web site and be up to date all the time.|

Leave a Reply

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