onBindViewHolder

abstract fun onBindViewHolder(@NonNull holder: VH, position: Int)(source)

Called by RecyclerView to display the data at the specified position. This method should update the contents of the itemView to reflect the item at the given position.

Note that unlike android.widget.ListView, RecyclerView will not call this method again if the position of the item changes in the data set unless the item itself is invalidated or the new position cannot be determined. For this reason, you should only use the position parameter while acquiring the related data item inside this method and should not keep a copy of it. If you need the position of an item later on (e.g. in a click listener), use getBindingAdapterPosition which will have the updated adapter position. Override onBindViewHolder instead if Adapter can handle efficient partial bind.

Parameters

holder

The ViewHolder which should be updated to represent the contents of the item at the given position in the data set.

position

The position of the item within the adapter's data set.


open fun onBindViewHolder(@NonNull holder: VH, position: Int, @NonNull payloads: List<Any>)(source)

Called by RecyclerView to display the data at the specified position. This method should update the contents of the itemView to reflect the item at the given position.

Note that unlike android.widget.ListView, RecyclerView will not call this method again if the position of the item changes in the data set unless the item itself is invalidated or the new position cannot be determined. For this reason, you should only use the position parameter while acquiring the related data item inside this method and should not keep a copy of it. If you need the position of an item later on (e.g. in a click listener), use getBindingAdapterPosition which will have the updated adapter position.

Partial bind vs full bind:

The payloads parameter is a merge list from notifyItemChanged or notifyItemRangeChanged. If the payloads list is not empty, the ViewHolder is currently bound to old data and Adapter may run an efficient partial update using the payload info. If the payload is empty, Adapter must run a full bind. Adapter should not assume that the payload passed in notify methods will be received by onBindViewHolder(). For example when the view is not attached to the screen, the payload in notifyItemChange() will be simply dropped.

Parameters

holder

The ViewHolder which should be updated to represent the contents of the item at the given position in the data set.

position

The position of the item within the adapter's data set.

payloads

A non-null list of merged payloads. Can be empty list if requires full update.