— Android, Kotlin, RecyclerView — 1 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.
To add a RecyclerView to your layout, you need to add the following code to the XML file:
1<androidx.recyclerview.widget.RecyclerView2 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.
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>() {3
4 class ViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)5
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 TextView9 return ViewHolder(textView)10 }11
12 override fun onBindViewHolder(holder: ViewHolder, position: Int) {13 holder.textView.text = dataset[position]14 }15
16 override fun getItemCount() = dataset.size17}
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.
To set up the RecyclerView in your activity or fragment, you need to do the following:
Here is an example of how to set up the RecyclerView in an activity:
1class MainActivity : AppCompatActivity() {2
3 private lateinit var recyclerView: RecyclerView4 private lateinit var adapter: MyAdapter5
6 override fun onCreate(savedInstanceState: Bundle?) {7 super.onCreate(savedInstanceState)8 setContentView(R.layout.activity_main)9
10 recyclerView = findViewById(R.id.recyclerView)11 adapter = MyAdapter(listOf("Item 1", "Item 2", "Item 3"))12 recyclerView.adapter = adapter13 recyclerView.layoutManager = LinearLayoutManager(this)14 }15}
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.