Visit Sponsor

Written by 7:48 am Android

Android RecyclerView Example with Adapter and ViewHolder

RecyclerView is a flexible and efficient component used to display large sets of data in Android applications. It is the modern replacement for ListView and GridView, offering better performance, view recycling, and layout flexibility.

This guide explains how to implement RecyclerView step by step using a clean architecture approach.

Why Use RecyclerView Instead of ListView?

RecyclerView provides:

  • Better performance through view recycling
  • ViewHolder pattern enforced by design
  • Multiple layout types (Linear, Grid, Staggered)
  • Item animations
  • Custom item decoration
  • Support for large datasets

In production applications, RecyclerView is the standard for rendering dynamic lists.

Step 1: Add RecyclerView Dependency

Add this dependency inside build.gradle:

implementation "androidx.recyclerview:recyclerview:1.3.2"

Always use the latest stable version available in AndroidX.

Step 2: Create Item Layout

Create a layout file: item_user.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:padding="12dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/tvEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

Step 3: Add RecyclerView in Activity Layout

activity_main.xml

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

Step 4: Create Data Model

public class User {
    private String name;
    private 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 RecyclerView Adapter

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

    private List<User> userList;

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

    @Override
    public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_user, parent, false);
        return new UserViewHolder(view);
    }

    @Override
    public void onBindViewHolder(UserViewHolder holder, int position) {
        User user = userList.get(position);
        holder.tvName.setText(user.getName());
        holder.tvEmail.setText(user.getEmail());
    }

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

    static class UserViewHolder extends RecyclerView.ViewHolder {

        TextView tvName, tvEmail;

        public UserViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            tvEmail = itemView.findViewById(R.id.tvEmail);
        }
    }
}

This follows the ViewHolder pattern for optimal performance.

Step 6: Initialize RecyclerView in Activity

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

recyclerView.setLayoutManager(new LinearLayoutManager(this));

List<User> userList = new ArrayList<>();
userList.add(new User("John Doe", "john@example.com"));
userList.add(new User("Jane Smith", "jane@example.com"));

UserAdapter adapter = new UserAdapter(userList);
recyclerView.setAdapter(adapter);

Now the RecyclerView will display the list of users.

Understanding LayoutManager

RecyclerView requires a LayoutManager to position items.

Common options:

  • LinearLayoutManager – Vertical or horizontal list
  • GridLayoutManager – Grid format
  • StaggeredGridLayoutManager – Pinterest-style layout

Example for Grid:

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

Adding Click Listener to RecyclerView Items

Modify Adapter:

holder.itemView.setOnClickListener(v -> {
    Toast.makeText(v.getContext(),
            user.getName(),
            Toast.LENGTH_SHORT).show();
});

This allows handling item interaction cleanly.

Performance Best Practices

From real production experience:

  • Always use ViewHolder pattern properly
  • Avoid heavy logic inside onBindViewHolder
  • Use DiffUtil for dynamic list updates
  • Use ListAdapter for better performance
  • Enable setHasFixedSize(true) if size does not change

Example:

recyclerView.setHasFixedSize(true);

Common Issues and Fixes

RecyclerView not showing data?

  • Check LayoutManager is set
  • Verify adapter is attached
  • Ensure item layout width is match_parent
  • Confirm dataset is not empty

Scroll lag?

  • Avoid complex layouts
  • Use ConstraintLayout for better performance
  • Minimize nested layouts

When to Use RecyclerView

Use RecyclerView for:

  • Lists
  • Grids
  • Dynamic content feeds
  • Chat messages
  • Product catalogs
  • Dashboard sections

It is the recommended list component for all modern Android applications.

Visited 4 times, 1 visit(s) today
Close