Junior — Middle
What actions are performed inside the onBindViewHolder method in RecyclerView?
sobes.tech AI
Answer from AI
The onBindViewHolder method binds data to the interface elements displayed in a specific ViewHolder. This method is called for each list item when the RecyclerView wants to display or update content.
Main actions inside onBindViewHolder:
- Retrieving data from the source (e.g., from a list of models) based on position.
- Filling the widgets inside the ViewHolder with this data (setting text, images, etc.).
- Setting up event handlers if needed.
Example:
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val item = dataList[position]
holder.textView.text = item.title
holder.imageView.setImageResource(item.imageResId)
}