Recycler View in Android
Recycler View in Android
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
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
}
XML
<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>
XML
<TextView
android:id="@+id/nameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@android:color/black" />
</LinearLayout>
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)
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
// This method returns the total number of items in the data set.
override fun getItemCount(): Int {
return nameList.size
}
}
7. MainActivity.kt
Kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
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!