Android Academy · Lesson

RecyclerView Basics

Display large lists efficiently with RecyclerView. Create an Adapter, ViewHolder, and connect data using ViewBinding.

Lesson 2 of 611 steps

RecyclerView Basics is a free Android Academy lesson on CoddyKit. This is lesson 2 of 6. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 6 lessons in total.

Why RecyclerView?

RecyclerView is the standard way to display a scrollable list of items in Android.

Unlike the older ListView, RecyclerView:

  • Recycles views as you scroll — only creates as many as fit on screen
  • Works for vertical lists, horizontal lists, and grids
  • Is highly customizable with LayoutManagers

RecyclerView Architecture

RecyclerView needs three pieces to work:

  • RecyclerView — the widget in your layout XML
  • Adapter — binds your data to item views
  • ViewHolder — holds references to the views inside each list item

The Adapter creates ViewHolders and binds data to them as the user scrolls.

Adding RecyclerView to XML

Add the widget to your layout and declare the dependency in build.gradle:

<!-- In res/layout/activity_main.xml -->
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- app/build.gradle dependencies: -->
<!-- implementation 'androidx.recyclerview:recyclerview:1.3.2' -->

Item Layout

Create a separate XML file for each list item. For example, res/layout/item_fruit.xml:

<!-- res/layout/item_fruit.xml -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:orientation="vertical">

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

    <TextView
        android:id="@+id/tvCalories"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp" />

</LinearLayout>

The Data Model

Define the data class that represents each item in your list:

data class Fruit(
    val name: String,
    val calories: Int
)

Creating the Adapter

The Adapter binds your data to the item layout. It has three key methods:

  • onCreateViewHolder — inflates the item XML
  • onBindViewHolder — fills in the data for one item
  • getItemCount — how many items total

Adapter Implementation

A complete, minimal RecyclerView Adapter:

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class FruitAdapter(
    private val fruits: List<Fruit>
) : RecyclerView.Adapter<FruitAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tvName: TextView = view.findViewById(R.id.tvName)
        val tvCalories: TextView = view.findViewById(R.id.tvCalories)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_fruit, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val fruit = fruits[position]
        holder.tvName.text = fruit.name
        holder.tvCalories.text = "${fruit.calories} kcal"
    }

    override fun getItemCount() = fruits.size
}

Setting Up in Activity

Wire the RecyclerView, Adapter, and LayoutManager together in your Activity:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    val fruits = listOf(
        Fruit("Apple", 95),
        Fruit("Banana", 105),
        Fruit("Cherry", 50),
        Fruit("Mango", 202)
    )

    val adapter = FruitAdapter(fruits)

    binding.recyclerView.apply {
        this.adapter = adapter
        layoutManager = LinearLayoutManager(this@MainActivity)
    }
}

GridLayoutManager

Change the LayoutManager to display a grid instead of a single-column list:

  • LinearLayoutManager — vertical or horizontal list
  • GridLayoutManager(context, spanCount) — grid with N columns
  • StaggeredGridLayoutManager — Pinterest-style staggered grid

Quick Check

What is the job of the ViewHolder in a RecyclerView Adapter?

Recap: RecyclerView Basics

You can now display lists efficiently:

  • RecyclerView recycles views for performance
  • Three parts: RecyclerView widget, Adapter, ViewHolder
  • Item XML defines one row/card appearance
  • Adapter connects data to views in onBindViewHolder
  • LayoutManager controls list, grid, or staggered layout

Next: make list items tappable with click events.

Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
36
Lessons
152

Frequently Asked Questions

Is the “RecyclerView Basics” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “RecyclerView Basics”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 6 lessons in total.

What will I learn in “RecyclerView Basics”?

Display large lists efficiently with RecyclerView. Create an Adapter, ViewHolder, and connect data using ViewBinding. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 6.

How long does the “RecyclerView Basics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Kotlin Collections
  2. RecyclerView Basics
  3. RecyclerView Click Events
  4. SharedPreferences
  5. DataStore Preferences
  6. Adapters & DiffUtil
← Back to Android Academy