Skip to content

Using RecyclerView in Android

Android, Kotlin, RecyclerView1 min read

The RecyclerView is a powerful widget introduced since Android 5.0 Lollipop that allows developers to efficiently display a large set of data on the screen by recycling the views when scrolling instead of creating new views for each item. This helps in improving the app's performance and reducing memory usage.

Here, we will discuss how to use the RecyclerView widget in an Android app using Kotlin. We will create a simple app that displays a list of items using the RecyclerView widget.

Adding RecyclerView to the Layout

To add a RecyclerView to your layout, you need to add the following code to the XML file:

2 android:id="@+id/recyclerView"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent" />

The androidx.recyclerview.widget.RecyclerView is the class that represents the RecyclerView widget. The android:id attribute assigns a unique ID to the RecyclerView, which can be used to reference it programmatically.

Creating a Custom Adapter

To display data in the RecyclerView, you need to create an adapter that binds the data to the view. You can create a custom adapter by extending the RecyclerView.Adapter class and implementing the onCreateViewHolder, onBindViewHolder, and getItemCount methods.

Here is an example of how to create a custom adapter:

1class MyAdapter(private val dataset: List<String>) :
2 RecyclerView.Adapter<MyAdapter.ViewHolder>() {
4 class ViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
6 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
7 val textView = LayoutInflater.from(parent.context)
8 .inflate(R.layout.item_view, parent, false) as TextView
9 return ViewHolder(textView)
10 }
12 override fun onBindViewHolder(holder: ViewHolder, position: Int) {
13 holder.textView.text = dataset[position]
14 }
16 override fun getItemCount() = dataset.size

In the onCreateViewHolder method, you inflate the layout for the item view and return a new instance of the ViewHolder class. In the onBindViewHolder method, you bind the data to the view. And in the getItemCount method, you return the number of items in the dataset.

Setting up the RecyclerView

To set up the RecyclerView in your activity or fragment, you need to do the following:

  1. Create a variable for the RecyclerView and initialize it.
  2. Create a variable for the adapter and initialize it with the dataset.
  3. Set the adapter to the RecyclerView.

Here is an example of how to set up the RecyclerView in an activity:

1class MainActivity : AppCompatActivity() {
3 private lateinit var recyclerView: RecyclerView
4 private lateinit var adapter: MyAdapter
6 override fun onCreate(savedInstanceState: Bundle?) {
7 super.onCreate(savedInstanceState)
8 setContentView(R.layout.activity_main)
10 recyclerView = findViewById(
11 adapter = MyAdapter(listOf("Item 1", "Item 2", "Item 3"))
12 recyclerView.adapter = adapter
13 recyclerView.layoutManager = LinearLayoutManager(this)
14 }

In the onCreate method, we find the RecyclerView by its ID and initialize it. We also create an instance of the adapter with some sample data and set it to the RecyclerView. Finally, we set the layout manager to be a LinearLayoutManager to display the items vertically.