Hey I am new in DiffUtil in adpater. I read some articles from stack overflow, google docs and some articles. I am trying to understand callback of DiffUtil areItemsTheSame and areContentsTheSame but, I am not clear what that means. I am adding some code, please have a look. If I am doing wrong please guide me.
GroupKey
data class GroupKey(
val type: EnumType,
val sender: Sender? = null,
val close: String? = null
)
EnumType
enum class EnumType {
A,
B
}
Sender
data class Sender(
val company: RoleType? = null,
val id: String? = null
)
RoleType
data class RoleType(
val name : String?= null
val id: String? = null
)
Group
data class Group(
val key: GroupKey,
val value: MutableList<Item?>
)
I am passing my list to adapter which is a Group mutableList
var messageGroupList: MutableList<Group>? = null
..
val adapter = MainAdapter()
binding.recylerview.adapter = adapter
adapter.submitList(groupList)
Using DiffUtil in adapter
MainAdapter.kt
class MainAdapter :ListAdapter<Group, RecyclerView.ViewHolder>(COMPARATOR) {
companion object {
private val COMPARATOR = object : DiffUtil.ItemCallback<Group>() {
override fun areItemsTheSame(oldItem: Group, newItem: Group): Boolean {
return oldItem == newItem
}
override fun areContentsTheSame(oldItem: Group, newItem: Group): Boolean {
return ((oldItem.value == newItem.value) && (oldItem.key == newItem.key))
}
}
}
.....
}
1. Here do I need to compare key other property like type, sender etc. also inside this DiffUtil.ItemCallback.
2. when to use == or === and what about equals()
3. If we compare int, boolean or String we use == or something else ?
Inside this adapter I am calling another Recyclerview with passing list of Item inside that adapter.
Item
data class Item(
val text: String? = null,
var isRead: Boolean? = null,
val sender: Sender? = null,
val id: Int? = null
)
NestedRecyclerView.kt
class NestedRecyclerView : ListAdapter<Item, IncomingMessagesViewHolder>(COMPARATOR) {
companion object {
private val COMPARATOR = object : DiffUtil.ItemCallback<Item>() {
override fun areItemsTheSame(oldItem: Item, newItem: Item): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Item, newItem: Item): Boolean {
return ((oldItem.isRead == oldItem.isRead) &&
(oldItem.sender == newItem.sender) &&
(oldItem.text == oldItem.text))
}
}
}
}
Again Same question Do I need to compare sender's other property here as well.
4. In areItemsTheSame do I need to compare id or just oldItem == newItem this?
5. How to proper way to update my adapter items. In normal reyclerview we use notifiyDataSetChanged. But in diffutil do I need to call again submitList function and it will take care of everything?
adapter.submitList(groupList)