Recycler View in Android is a powerful and flexible widget for displaying large sets of data.
Here's a simple example of how to use it in Kotlin, broken down step-by-step. We will create
a list of names and display them.
1. Project Setup
First, create a new Android Studio project with an "Empty Activity."
2. Add RecyclerView Dependency
Open your build.gradle (Module: app) file and add the following dependency inside the
dependencies block. Make sure to sync your project after adding it.
Gradle
dependencies {
// ... other dependencies
implementation "androidx.recyclerview:recyclerview:1.3.3" // Use the latest stable version
}
3. Layout File (activity_main.xml)
Open activity_main.xml (located in app/src/main/res/layout) and replace its content with a
RecyclerView.
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
4. Item Layout File (list_item.xml)
Create a new XML layout file named list_item.xml in the app/src/main/res/layout directory.
This file will define the layout for each individual item in our list. We'll just have a simple
TextView.
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">
<TextView
android:id="@+id/nameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@android:color/black" />
</LinearLayout>
5. Data Class (Optional but Recommended)
It's good practice to define a data class for your items. In this simple case, we are just using
String for names but for more complex objects, this is very useful.
Kotlin
// No separate file needed for this simple example. You can define it directly in MainActivity.
// data class Name(val value: String)
6. RecyclerView Adapter (MyAdapter.kt)
Create a new Kotlin file named MyAdapter.kt (right-click on your package -> New -> Kotlin
Class/File). This adapter acts as a bridge between your data and the RecyclerView.
Kotlin
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class MyAdapter(private val nameList: List<String>) :
RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
// This class holds the views for each item in the RecyclerView
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val nameTextView: TextView = itemView.findViewById(R.id.nameTextView)
}
// This method is called when the RecyclerView needs a new ViewHolder.
// It inflates the item layout and creates a MyViewHolder.
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val itemView = LayoutInflater.from(parent.context)
.inflate(R.layout.list_item, parent, false)
return MyViewHolder(itemView)
}
// This method binds the data to the views in each item.
// It's called to display the data at the specified position.
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val currentName = nameList[position]
holder.nameTextView.text = currentName
}
// This method returns the total number of items in the data set.
override fun getItemCount(): Int {
return nameList.size
}
}
7. MainActivity.kt
Finally, open MainActivity.kt and set up the RecyclerView.
Kotlin
package com.example.myrecyclerviewapp // Replace with your actual package name
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var nameAdapter: MyAdapter
private val names = mutableListOf<String>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView = findViewById(R.id.recyclerView)
// Prepare your data
names.add("Alice")
names.add("Bob")
names.add("Charlie")
names.add("David")
names.add("Eve")
names.add("Frank")
names.add("Grace")
names.add("Heidi")
names.add("Ivan")
names.add("Judy")
// Create an instance of your adapter
nameAdapter = MyAdapter(names)
// Set the adapter to the RecyclerView
recyclerView.adapter = nameAdapter
// Set a layout manager. LinearLayoutManager displays items in a vertical or horizontal list.
recyclerView.layoutManager = LinearLayoutManager(this)
// You can also add more items dynamically
// names.add("New Name")
// nameAdapter.notifyDataSetChanged() // Notify the adapter that the data has changed
}
}
Explanation:
● RecyclerView in activity_main.xml: This is the container that will hold and display
your list items.
● list_item.xml: This defines how each individual item in the list will look. Here, it's just
a TextView to display a name.
● MyAdapter.kt:
○ MyViewHolder: This inner class holds references to the views within each list
item (in our case, the nameTextView). It helps to efficiently reuse views.
○ onCreateViewHolder(): This method is called when the RecyclerView needs a
new ViewHolder. It inflates (R.layout.list_item) your item layout and creates a
new MyViewHolder for it.
○ onBindViewHolder(): This is where you actually populate the views with data.
It's called to display the data at the specified position in your list.
○ getItemCount(): This method tells the RecyclerView how many items are in
your data set.
● MainActivity.kt:
○ We get a reference to our recyclerView from the layout.
○ We create a list of String objects (names) that will be displayed.
○ We create an instance of our MyAdapter, passing our names list to it.
○ recyclerView.adapter = nameAdapter: We tell the RecyclerView to use our
custom adapter.
○ recyclerView.layoutManager = LinearLayoutManager(this): A LayoutManager
is essential for a RecyclerView. It determines how the items are arranged
(e.g., as a vertical list, horizontal list, or grid). LinearLayoutManager is the
simplest, creating a vertical scrolling list by default.
Now, run your application, and you should see a scrollable list of names!