Skip to Content

RecyclerView Complete Tutorial in Android (Java)

Introduction

RecyclerView is one of the most important UI components in Android development. It is used to display large amounts of data efficiently in a list or grid format.

RecyclerView is the modern replacement for ListView because it provides:

  • Better Performance

  • View Recycling

  • Custom Layouts

  • Animations

  • Grid Support

  • Horizontal Lists

In this tutorial, you will learn how to implement RecyclerView in Android using Java from scratch.


What is RecyclerView?

RecyclerView is a flexible Android widget used to display collections of data.

Examples:

  • WhatsApp Chat List

  • YouTube Video Feed

  • Instagram Posts

  • Play Store Apps

  • Product Listings

All these applications use RecyclerView.


Prerequisites

Before starting:

  • Android Studio

  • Basic Java Knowledge

  • Android Project


Step 1: Add RecyclerView Dependency

Modern Android projects already include RecyclerView.

If not, add:

implementation 'androidx.recyclerview:recyclerview:1.3.2'

Sync project.


Step 2: Create Layout

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

Step 3: Create Item Layout

item_user.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="12dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/txtName"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/txtEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

Step 4: Create Model Class

User.java

public class User {

    String name;
    String email;

    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

}

Step 5: Create Adapter Class

UserAdapter.java

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class UserAdapter
        extends RecyclerView.Adapter<UserAdapter.ViewHolder> {

    List<User> userList;

    public UserAdapter(List<User> userList) {
        this.userList = userList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(
            @NonNull ViewGroup parent,
            int viewType) {

        View view =
                LayoutInflater.from(parent.getContext())
                        .inflate(
                                R.layout.item_user,
                                parent,
                                false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(
            @NonNull ViewHolder holder,
            int position) {

        User user =
                userList.get(position);

        holder.txtName.setText(
                user.getName());

        holder.txtEmail.setText(
                user.getEmail());

    }

    @Override
    public int getItemCount() {
        return userList.size();
    }

    public static class ViewHolder
            extends RecyclerView.ViewHolder {

        TextView txtName, txtEmail;

        public ViewHolder(
                @NonNull View itemView) {

            super(itemView);

            txtName =
                    itemView.findViewById(
                            R.id.txtName);

            txtEmail =
                    itemView.findViewById(
                            R.id.txtEmail);
        }
    }
}

Understanding Adapter

Adapter acts as a bridge between:

Data
 ↓
Adapter
 ↓
RecyclerView

It tells RecyclerView:

  • How many items exist

  • Which layout to use

  • What data to display


Step 6: Setup RecyclerView

MainActivity.java

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity
        extends AppCompatActivity {

    RecyclerView recyclerView;

    List<User> userList;

    UserAdapter adapter;

    @Override
    protected void onCreate(
            Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(
                R.layout.activity_main);

        recyclerView =
                findViewById(
                        R.id.recyclerView);

        userList =
                new ArrayList<>();

        userList.add(
                new User(
                        "Anoj Kumar",
                        "anoj@gmail.com"));

        userList.add(
                new User(
                        "Rahul",
                        "rahul@gmail.com"));

        userList.add(
                new User(
                        "Amit",
                        "amit@gmail.com"));

        adapter =
                new UserAdapter(userList);

        recyclerView.setLayoutManager(
                new LinearLayoutManager(this));

        recyclerView.setAdapter(adapter);

    }
}

Output

RecyclerView displays:

Anoj Kumar
anoj@gmail.com

Rahul
rahul@gmail.com

Amit
amit@gmail.com

as a scrolling list.


RecyclerView Lifecycle

onCreateViewHolder()

Creates row layout.

onCreateViewHolder()

Runs only when needed.


onBindViewHolder()

Binds data.

onBindViewHolder()

Called repeatedly while scrolling.


getItemCount()

Returns total items.

getItemCount()

View Recycling

RecyclerView does not create unlimited views.

Example:

1000 items

RecyclerView may create only:

10–15 Views

and reuse them.

This is why RecyclerView is fast.


Item Click Listener

Inside Adapter:

holder.itemView.setOnClickListener(v -> {

    Toast.makeText(
            v.getContext(),
            user.getName(),
            Toast.LENGTH_SHORT
    ).show();

});

Now clicking any item shows its name.


Horizontal RecyclerView

LinearLayoutManager manager =
        new LinearLayoutManager(
                this,
                LinearLayoutManager.HORIZONTAL,
                false);

recyclerView.setLayoutManager(manager);

Grid RecyclerView

recyclerView.setLayoutManager(
        new GridLayoutManager(
                this,
                2));

Displays:

Item Item
Item Item
Item Item

Add New Data Dynamically

userList.add(
        new User(
                "New User",
                "new@gmail.com"));

adapter.notifyDataSetChanged();

RecyclerView updates instantly.


Remove Item

userList.remove(position);

adapter.notifyItemRemoved(position);

Update Item

userList.set(
        position,
        new User(
                "Updated",
                "updated@gmail.com"));

adapter.notifyItemChanged(position);

Common Errors

RecyclerView Not Showing

Cause:

setLayoutManager()

missing.

Solution:

Always set LayoutManager.


App Crash

Cause:

findViewById()

wrong ID.

Solution:

Verify XML IDs.


Empty RecyclerView

Cause:

userList

contains no data.

Solution:

Add items before setting adapter.


Best Practices

✅ Use RecyclerView instead of ListView

✅ Use ViewHolder Pattern

✅ Use DiffUtil for large lists

✅ Use Pagination

✅ Use Glide/Picasso for images

✅ Keep item layouts lightweight


Real World Examples

RecyclerView is used in:

  • WhatsApp Chats

  • Instagram Feed

  • YouTube Videos

  • Play Store Apps

  • Amazon Products

  • Facebook Posts

Almost every Android application uses RecyclerView.


Complete Flow

Model
 ↓
Data List
 ↓
Adapter
 ↓
ViewHolder
 ↓
RecyclerView
 ↓
UI

Conclusion

Congratulations! 🎉

You have successfully learned RecyclerView in Android using Java.

You now know:

  • RecyclerView Basics

  • Adapter

  • ViewHolder

  • Model Class

  • Item Layout

  • Click Events

  • Grid Layout

  • Horizontal Layout

  • Dynamic Updates

RecyclerView is one of the most important Android concepts and is used in almost every real-world application.

Next Tutorial

  • RecyclerView with Firebase

  • RecyclerView with Retrofit API

  • RecyclerView Search Filter

  • RecyclerView Pagination

  • RecyclerView Multiple View Types

  • MVVM Architecture in Android


Disclaimer: The information, code snippets, and tutorials presented on this website are for educational and informational purposes only. While we make every effort to provide accurate, tested, and high-quality contents, all materials are provided "as is" without warranty of any kind. Use them at your own discretion and risk.

Enjoyed this tutorial?

Consider supporting Pal Coder so we can keep creating high-quality, free Android development resources.