Paramclasses Academy - Blog

Tutorials Details

←
Post 20 of 40
→

How to Use RecyclerView and Adapters to Display Lists in Android Apps ?

How to Use RecyclerView and Adapters to Display Lists in Android Apps ?

November 3, 2026

1. Introduction

Displaying lists is a common requirement in Android applications.

For example:

  • Student Management Apps display student records.

  • Shopping Apps display product lists.

  • Contact Apps display names and phone numbers.

  • News Apps display articles.

  • Chat Apps display messages.

Android provides a powerful component called RecyclerView to display these types of lists efficiently.

In this tutorial, we will learn how RecyclerView works, what an Adapter does, how ViewHolder manages item views, and how to create a practical Student List App using Java and XML.

By the end, you will be able to display multiple student records in a scrollable list.

2. What Is RecyclerView in Android?

RecyclerView is an Android UI component used to display large or dynamic collections of data efficiently.

It reuses item views instead of creating a completely new view for every item whenever the user scrolls.

For example, imagine an application containing 500 student records.

Displaying and managing every row independently can waste resources. RecyclerView reuses views that move off the screen to display new records.

This process is called View Recycling.

Advantages of RecyclerView

  • Efficiently displays large lists.

  • Reuses existing item views.

  • Supports vertical and horizontal lists.

  • Supports grid layouts.

  • Allows custom item designs.

  • Works with different types of data.

3. Understanding the Main Components of RecyclerView

RecyclerView uses three important components.

3.1 RecyclerView

RecyclerView is the main container that displays the list items.

It manages scrolling and requests item views when required.

Example:

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

3.2 Adapter

An Adapter connects the data source with RecyclerView.

It takes information from the data list and displays that information inside the item layout.

For example, if we have 10 student records, the Adapter helps RecyclerView display the correct student information in each row.

3.3 ViewHolder

ViewHolder holds references to the views used in one list item.

For example, one student row may contain:

  • Student Name

  • Student Email

The ViewHolder stores references to these TextViews so the Adapter can update them efficiently.

4. How RecyclerView Works

RecyclerView follows a simple process:

  1. We create a data list.

  2. We create an XML layout for one item.

  3. We create an Adapter.

  4. The Adapter creates ViewHolder objects.

  5. RecyclerView requests data for each visible item.

  6. The Adapter binds the correct data to each ViewHolder.

  7. When the user scrolls, RecyclerView reuses item views where possible.

Three Adapter methods are especially important.

onCreateViewHolder()

Creates a new ViewHolder and inflates the item XML layout when RecyclerView needs another item view.

onBindViewHolder()

Places the correct data into the views held by a ViewHolder.

getItemCount()

Returns the total number of items in the data list.

5. What Is LayoutManager?

LayoutManager controls how items are arranged inside RecyclerView.

Android provides three commonly used LayoutManagers.

5.1 LinearLayoutManager

Displays items in a vertical or horizontal list.

Vertical example:

recyclerView.setLayoutManager(
    new LinearLayoutManager(this)
);

Horizontal example:

recyclerView.setLayoutManager(
    new LinearLayoutManager(
        this,
        LinearLayoutManager.HORIZONTAL,
        false
    )
);

5.2 GridLayoutManager

Displays items in a grid with a specified number of columns.

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

Here, 2 means the grid has two columns.

5.3 StaggeredGridLayoutManager

Displays items in a staggered grid, allowing item dimensions to vary.

recyclerView.setLayoutManager(
    new StaggeredGridLayoutManager(
        2,
        StaggeredGridLayoutManager.VERTICAL
    )
);

For our Student List App, we will use LinearLayoutManager.

6. Practical Project: Create a Student List App

Now let's create a practical Android application that displays multiple student records using RecyclerView and a custom Adapter.

Our application will display:

  • Student Name

  • Student Email

Each student will appear in a separate list item.

Step 1: Create a New Android Studio Project

  1. Open Android Studio.

  2. Select New Project.

  3. Choose Empty Views Activity.

  4. Enter the project name RecyclerViewApp.

  5. Select Java as the programming language.

  6. Click Finish.

Step 2: Add RecyclerView Dependency

Open the app-level Gradle file.

Depending on your project, it may be named:

app/build.gradle

or:

app/build.gradle.kts

For a Groovy build.gradle file, add this inside the dependencies block:

implementation 'androidx.recyclerview:recyclerview:1.4.0'

For a Kotlin DSL build.gradle.kts file, use:

implementation("androidx.recyclerview:recyclerview:1.4.0")

Now click Sync Now.

This dependency provides the RecyclerView library.

Step 3: Create the Main XML Layout

Open:

app > res > layout > activity_main.xml

Replace the existing XML code with:

<?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"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Student List"
        android:textSize="26sp"
        android:textStyle="bold"
        android:gravity="center"
        android:padding="15dp" />

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

</LinearLayout>

Understanding the XML:

  • LinearLayout arranges the screen components vertically.

  • TextView displays the Student List heading.

  • RecyclerView is the container for student records.

  • layout_weight="1" allows RecyclerView to occupy the remaining available height.

Step 4: Create the Student Item Layout

Each student record needs its own layout.

Navigate to:

app > res > layout

Right-click the layout folder and select:

New > Layout Resource File

Set the file name to:

item_student

Now add this 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="wrap_content"
    android:orientation="vertical"
    android:padding="16dp"
    android:layout_marginBottom="10dp"
    android:background="#F1F1F1">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Student Name"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="#222222" />

    <TextView
        android:id="@+id/tvEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="student@example.com"
        android:textSize="14sp"
        android:textColor="#555555"
        android:layout_marginTop="5dp" />

</LinearLayout>

Understanding the item layout:

  • tvName displays the student's name.

  • tvEmail displays the student's email.

  • padding creates space inside the item.

  • layout_marginBottom creates space between list items.

  • background gives each item a light background.

This layout will be reused for every student record.

Step 5: Create a Student Java Class

Now we need a class to represent student information.

Navigate to your Java package.

Right-click the package and select:

New > Java Class

Name the class:

Student

Add this code:

public class Student {

    private String name;
    private String email;

    public Student(String name, String email) {

        this.name = name;
        this.email = email;

    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

}

Understanding the Student Class:

  • name stores the student's name.

  • email stores the student's email.

  • The constructor initializes the student information.

  • getName() returns the student name.

  • getEmail() returns the student email.

For example:

Student student = new Student(
    "Rahul Sharma",
    "rahul@gmail.com"
);

This creates one Student object.

Step 6: Create the RecyclerView Adapter

Now we will create the most important part of this project: the Adapter.

Right-click your Java package and select:

New > Java Class

Name the class:

StudentAdapter

Add the following code:

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 StudentAdapter
        extends RecyclerView.Adapter<StudentAdapter.StudentViewHolder> {

    private List<Student> studentList;

    public StudentAdapter(List<Student> studentList) {

        this.studentList = studentList;

    }

    // ViewHolder class
    public static class StudentViewHolder
            extends RecyclerView.ViewHolder {

        TextView tvName, tvEmail;

        public StudentViewHolder(@NonNull View itemView) {

            super(itemView);

            tvName = itemView.findViewById(R.id.tvName);
            tvEmail = itemView.findViewById(R.id.tvEmail);

        }
    }

    // Create item views
    @NonNull
    @Override
    public StudentViewHolder onCreateViewHolder(
            @NonNull ViewGroup parent,
            int viewType) {

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

        return new StudentViewHolder(view);

    }

    // Bind data to item views
    @Override
    public void onBindViewHolder(
            @NonNull StudentViewHolder holder,
            int position) {

        Student student = studentList.get(position);

        holder.tvName.setText(student.getName());
        holder.tvEmail.setText(student.getEmail());

    }

    // Return total number of items
    @Override
    public int getItemCount() {

        return studentList.size();

    }

}

Understanding the Adapter Code:

1. Extending RecyclerView.Adapter

extends RecyclerView.Adapter<StudentAdapter.StudentViewHolder>

This makes StudentAdapter a RecyclerView Adapter.

2. Creating the ViewHolder

public static class StudentViewHolder
        extends RecyclerView.ViewHolder

This class holds references to the TextViews inside one student item.

3. Inflating the Item Layout

.inflate(R.layout.item_student, parent, false);

This loads the item_student.xml layout whenever RecyclerView needs a new item view.

4. Binding Student Data

Student student = studentList.get(position);

holder.tvName.setText(student.getName());
holder.tvEmail.setText(student.getEmail());

This retrieves the student at the given position and displays the student's name and email.

5. Returning Item Count

return studentList.size();

This tells RecyclerView how many records are available.

Step 7: Write MainActivity.java Code

Now open:

app > java > your package name > MainActivity.java

Add the following code:

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;

    StudentAdapter studentAdapter;

    List<Student> studentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // Connect RecyclerView
        recyclerView = findViewById(R.id.recyclerView);

        // Create student list
        studentList = new ArrayList<>();

        // Add student records
        studentList.add(
                new Student("Rahul Sharma", "rahul@gmail.com")
        );

        studentList.add(
                new Student("Amit Verma", "amit@gmail.com")
        );

        studentList.add(
                new Student("Priya Singh", "priya@gmail.com")
        );

        studentList.add(
                new Student("Neha Patel", "neha@gmail.com")
        );

        studentList.add(
                new Student("Rohit Kumar", "rohit@gmail.com")
        );

        studentList.add(
                new Student("Anjali Gupta", "anjali@gmail.com")
        );

        // Set LayoutManager
        recyclerView.setLayoutManager(
                new LinearLayoutManager(this)
        );

        // Create Adapter
        studentAdapter = new StudentAdapter(studentList);

        // Set Adapter
        recyclerView.setAdapter(studentAdapter);

    }
}

Important: Keep the actual package declaration generated by Android Studio at the top of each Java file. The import statements above are only the required imports; your package name may be different.

Step 8: Run the Application

Now run your application on an emulator or Android phone.

You should see:

  • Student List heading.

  • Rahul Sharma with email.

  • Amit Verma with email.

  • Priya Singh with email.

  • Neha Patel with email.

  • Rohit Kumar with email.

  • Anjali Gupta with email.

Scroll the screen to see the remaining records.

You have successfully created a RecyclerView application using Java.

7. How to Display RecyclerView Items Horizontally

RecyclerView can also display items horizontally.

In MainActivity.java, replace:

recyclerView.setLayoutManager(
    new LinearLayoutManager(this)
);

With:

recyclerView.setLayoutManager(
    new LinearLayoutManager(
        this,
        LinearLayoutManager.HORIZONTAL,
        false
    )
);

Now the student items will appear in a horizontal scrolling list.

8. How to Display RecyclerView Items in a Grid

If you want to display items in two columns, use GridLayoutManager.

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

Also add this import:

import androidx.recyclerview.widget.GridLayoutManager;

Now RecyclerView will arrange the items in two columns.

9. Common Mistakes to Avoid

Mistake 1: Forgetting the RecyclerView Dependency

If Android Studio cannot find the RecyclerView class, check the app-level Gradle dependency and sync the project.

Mistake 2: Incorrect Item Layout ID

Make sure the layout name is correct:

R.layout.item_student

Mistake 3: Incorrect View IDs

The IDs used in the Adapter must match the IDs in item_student.xml.

R.id.tvName
R.id.tvEmail

Mistake 4: Forgetting to Set LayoutManager

RecyclerView needs a LayoutManager to arrange its items.

recyclerView.setLayoutManager(
    new LinearLayoutManager(this)
);

Mistake 5: Forgetting to Set Adapter

Without setting the Adapter, the list data will not be displayed.

recyclerView.setAdapter(studentAdapter);

Mistake 6: Incorrect getItemCount()

Always return the number of records in your data list.

return studentList.size();

10. Practice Tasks

Now try these tasks yourself:

  1. Add five more student records.

  2. Add a Student ID field to the Student class.

  3. Display Student ID in every RecyclerView item.

  4. Change the vertical list into a horizontal list.

  5. Display student records in a two-column grid.

  6. Change the text size and background of each item.

  7. Add a click listener to each student item and display the selected student's name in a Toast.

Try completing these tasks without copying the code immediately.

11. Conclusion

In this tutorial, we learned how to display lists using RecyclerView and Adapter in Android Studio with Java.

We covered:

  • What RecyclerView is.

  • Why RecyclerView reuses item views.

  • What an Adapter does.

  • Understanding ViewHolder.

  • The three important Adapter methods.

  • LinearLayoutManager.

  • GridLayoutManager.

  • StaggeredGridLayoutManager.

  • Creating a Student model class.

  • Designing a custom item layout.

  • Building a complete Student List App.

RecyclerView is an important component in Android development. You will use it in student management apps, product lists, contact apps, news applications and many other real-world projects.

←
Post 20 of 40
→