0Pricing
Android Academy · Lesson

RecyclerView Basics

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

RecyclerView Basics is a free Android Academy lesson on CoddyKit — lesson 2 of 6. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “RecyclerView Basics” lesson free?

Yes — the full text of “RecyclerView Basics” is free to read here on the web, and the Android Academy course includes 6 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 6, so you can start here or from the beginning and move at your own pace.

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