Android
RecyclerView
Programming
Tutorial
Mobile Development

Simple Android RecyclerView example

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

RecyclerView is the standard Android widget for rendering scrollable lists efficiently. A minimal setup needs an item layout, an adapter, a view holder, and wiring in an Activity or Fragment. This guide shows a simple Kotlin example that you can run and extend for production features like click handling and diff updates.

Add RecyclerView Dependency and Layout

Most modern Android templates already include RecyclerView through AndroidX, but verify it is available.

gradle
dependencies {
    implementation("androidx.recyclerview:recyclerview:1.3.2")
}

Create an Activity layout with one RecyclerView.

xml
1<!-- res/layout/activity_main.xml -->
2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3    xmlns:app="http://schemas.android.com/apk/res-auto"
4    android:layout_width="match_parent"
5    android:layout_height="match_parent">
6
7    <androidx.recyclerview.widget.RecyclerView
8        android:id="@+id/recyclerView"
9        android:layout_width="0dp"
10        android:layout_height="0dp"
11        app:layout_constraintTop_toTopOf="parent"
12        app:layout_constraintBottom_toBottomOf="parent"
13        app:layout_constraintStart_toStartOf="parent"
14        app:layout_constraintEnd_toEndOf="parent" />
15
16</androidx.constraintlayout.widget.ConstraintLayout>

Create a Simple Row Layout

Define a row with one TextView.

xml
1<!-- res/layout/item_simple.xml -->
2<TextView xmlns:android="http://schemas.android.com/apk/res/android"
3    android:id="@+id/titleText"
4    android:layout_width="match_parent"
5    android:layout_height="wrap_content"
6    android:padding="16dp"
7    android:textSize="16sp" />

Implement the Adapter and ViewHolder

Keep the first version small and readable.

kotlin
1import android.view.LayoutInflater
2import android.view.View
3import android.view.ViewGroup
4import android.widget.TextView
5import androidx.recyclerview.widget.RecyclerView
6
7class SimpleAdapter(
8    private val items: List<String>
9) : RecyclerView.Adapter<SimpleAdapter.SimpleViewHolder>() {
10
11    class SimpleViewHolder(view: View) : RecyclerView.ViewHolder(view) {
12        val title: TextView = view.findViewById(R.id.titleText)
13    }
14
15    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SimpleViewHolder {
16        val view = LayoutInflater.from(parent.context)
17            .inflate(R.layout.item_simple, parent, false)
18        return SimpleViewHolder(view)
19    }
20
21    override fun onBindViewHolder(holder: SimpleViewHolder, position: Int) {
22        holder.title.text = items[position]
23    }
24
25    override fun getItemCount(): Int = items.size
26}

Wire RecyclerView in Activity

Set a layout manager and attach adapter data.

kotlin
1import android.os.Bundle
2import androidx.appcompat.app.AppCompatActivity
3import androidx.recyclerview.widget.LinearLayoutManager
4import androidx.recyclerview.widget.RecyclerView
5
6class MainActivity : AppCompatActivity() {
7    override fun onCreate(savedInstanceState: Bundle?) {
8        super.onCreate(savedInstanceState)
9        setContentView(R.layout.activity_main)
10
11        val data = listOf("Alpha", "Beta", "Gamma", "Delta", "Epsilon")
12
13        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
14        recyclerView.layoutManager = LinearLayoutManager(this)
15        recyclerView.adapter = SimpleAdapter(data)
16    }
17}

This is enough to render a performant scrolling list.

Add Click Handling and Update Strategy

A common next step is passing a click callback into the adapter.

kotlin
1class SimpleAdapter(
2    private val items: List<String>,
3    private val onClick: (String) -> Unit
4) : RecyclerView.Adapter<SimpleAdapter.SimpleViewHolder>() {
5
6    // same ViewHolder and create methods
7
8    override fun onBindViewHolder(holder: SimpleViewHolder, position: Int) {
9        val item = items[position]
10        holder.title.text = item
11        holder.itemView.setOnClickListener { onClick(item) }
12    }
13
14    override fun getItemCount(): Int = items.size
15}

For dynamic lists, prefer ListAdapter and DiffUtil so UI updates are efficient and animated correctly.

Upgrade to ListAdapter for Dynamic Data

When list content changes frequently, ListAdapter with DiffUtil computes minimal updates and avoids full list redraws.

kotlin
1import androidx.recyclerview.widget.DiffUtil
2import androidx.recyclerview.widget.ListAdapter
3
4class NameAdapter : ListAdapter<String, SimpleAdapter.SimpleViewHolder>(DIFF) {
5    companion object {
6        val DIFF = object : DiffUtil.ItemCallback<String>() {
7            override fun areItemsTheSame(oldItem: String, newItem: String): Boolean = oldItem == newItem
8            override fun areContentsTheSame(oldItem: String, newItem: String): Boolean = oldItem == newItem
9        }
10    }
11
12    // onCreateViewHolder and onBindViewHolder remain similar
13}

This change improves scrolling smoothness and update behavior when filters or network refreshes modify the list. It also reduces visual flicker when items are inserted, removed, or reordered during live updates.

Common Pitfalls

  • Forgetting to set a LayoutManager, which prevents items from rendering.
  • Inflating row layout with incorrect parent attach behavior.
  • Performing heavy work inside onBindViewHolder, causing scroll jank.
  • Updating backing data without notifying adapter or using diff-based adapters.
  • Holding Activity references in adapters longer than needed.

Summary

  • A minimal RecyclerView needs layout, adapter, view holder, and layout manager.
  • Keep row binding logic light for smooth scrolling.
  • Add click callbacks through adapter constructor parameters.
  • Move to ListAdapter with DiffUtil for real-world dynamic data.
  • Treat RecyclerView setup as foundation for pagination, filtering, and richer rows in production apps.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.