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 [Link] (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 "[Link]: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"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<[Link]
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" />
</[Link]>
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="[Link]
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 ([Link])
Create a new Kotlin file named [Link] (right-click on your package -> New -> Kotlin
Class/File). This adapter acts as a bridge between your data and the RecyclerView.
Kotlin
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MyAdapter(private val nameList: List<String>) :
[Link]<[Link]>() {
// This class holds the views for each item in the RecyclerView
class MyViewHolder(itemView: View) : [Link](itemView) {
val nameTextView: TextView = [Link]([Link])
}
// 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 = [Link]([Link])
.inflate([Link].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]
[Link] = currentName
}
// This method returns the total number of items in the data set.
override fun getItemCount(): Int {
return [Link]
}
}
7. [Link]
Finally, open [Link] and set up the RecyclerView.
Kotlin
package [Link] // Replace with your actual package name
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var nameAdapter: MyAdapter
private val names = mutableListOf<String>()
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
recyclerView = findViewById([Link])
// Prepare your data
[Link]("Alice")
[Link]("Bob")
[Link]("Charlie")
[Link]("David")
[Link]("Eve")
[Link]("Frank")
[Link]("Grace")
[Link]("Heidi")
[Link]("Ivan")
[Link]("Judy")
// Create an instance of your adapter
nameAdapter = MyAdapter(names)
// Set the adapter to the RecyclerView
[Link] = nameAdapter
// Set a layout manager. LinearLayoutManager displays items in a vertical or horizontal list.
[Link] = LinearLayoutManager(this)
// You can also add more items dynamically
// [Link]("New Name")
// [Link]() // 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.
● [Link]:
○ 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 ([Link].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.
● [Link]:
○ 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.
○ [Link] = nameAdapter: We tell the RecyclerView to use our
custom adapter.
○ [Link] = 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!