Implementing RecyclerView in Android Apps
Q: How do you implement a RecyclerView in an Android app? Can you show an example of how to bind data to it?
- Android
- Junior level question
Explore all the latest Android interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Android interview for FREE!
To implement a RecyclerView in an Android app, you need to follow these steps:
- Add
the RecyclerView dependency to your project's build.gradle
file:
implementation 'androidx.recyclerview:recyclerview:x.x.x'
- Add
the RecyclerView widget to your XML layout file:
<androidx.recyclerview.widget.RecyclerView android:id="@+id/myRecyclerView" android:layout_width="match_parent" android:layout_height="match_parent" />
- Create
a layout file for each item in the RecyclerView. This layout will
be inflated for each item in the list:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/itemTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/itemSubtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
- Create
a ViewHolder class that holds references to the views in each item layout:
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val titleTextView: TextView = itemView.findViewById(R.id.itemTitle) val subtitleTextView: TextView = itemView.findViewById(R.id.itemSubtitle) }
- Create
an Adapter class that extends RecyclerView.Adapter<MyViewHolder>.
This class will be responsible for binding data to the RecyclerView:
class MyAdapter(private val itemList: List<Item>) : RecyclerView.Adapter<MyViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false) return MyViewHolder(itemView) } override fun onBindViewHolder(holder: MyViewHolder, position: Int) { val currentItem = itemList[position] holder.titleTextView.text = currentItem.title holder.subtitleTextView.text = currentItem.subtitle } override fun getItemCount() = itemList.size }
- Set
the adapter for the RecyclerView in your activity or fragment:
val itemList = listOf( Item("Item 1", "Subtitle 1"), Item("Item 2", "Subtitle 2"), Item("Item 3", "Subtitle 3") ) val recyclerView = findViewById<RecyclerView>(R.id.myRecyclerView) recyclerView.adapter = MyAdapter(itemList)
In this example, we have a simple RecyclerView with
two text views per item. The MyViewHolder class holds references to the
views in each item layout. The MyAdapter class binds data to the views
in each item layout, and the getItemCount() method returns the number of
items in the list. Finally, in the activity or fragment, we create a list of
items and set the adapter for the RecyclerView. The adapter inflates the
item layout for each item in the list, and binds data to the views in each item
layout.


