嘿,我是Adpater中DiffUtil 的新手。我從堆疊溢位、谷歌檔案和一些文章中閱讀了一些文章。我試圖了解DiffUtil areItemsTheSame和areContentsTheSame 的回呼,但是,我不清楚這意味著什么。我正在添加一些代碼,請看一下。如果我做錯了,請指導我。
組密鑰
data class GroupKey(
val type: EnumType,
val sender: Sender? = null,
val close: String? = null
)
列舉型別
enum class EnumType {
A,
B
}
發件人
data class Sender(
val company: RoleType? = null,
val id: String? = null
)
角色型別
data class RoleType(
val name : String?= null
val id: String? = null
)
團體
data class Group(
val key: GroupKey,
val value: MutableList<Item?>
)
我將我的串列傳遞給配接器,它是一個Group mutableList
var messageGroupList: MutableList<Group>? = null
..
val adapter = MainAdapter()
binding.recylerview.adapter = adapter
adapter.submitList(groupList)
在配接器中使用DiffUtil
主配接器.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.在這里,我是否需要在此 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.如何以正確的方式更新我的配接器專案。在正常的 reyclerview 中,我們使用notifyDataSetChanged。但是在diffutil 中我是否需要再次呼叫submitList函式,它會處理一切嗎?
adapter.submitList(groupList)
uj5u.com熱心網友回復:
問題 1 和 4:
areItemsTheSame意味著兩個實體表示相同的資料項,即使某些內容可能不同。假設您有一個聯系人串列,并且 Jane 的中間名首字母已更改,但該行仍應代表同一個人 Jane。您的模型類可能有不同的實體,具有一些不同的值,但它們應該代表同一行。
因此,通常您只會比較新舊專案之間的一件事,在這種情況下,它們中的每一個都是相同的。通常,如果您從資料庫或 API 獲取資料,則會有一些代表資料點的唯一 ID,這就是您需要在areItemsTheSame. 例如,oldItem.id == newItem.id。
areContentsTheSame意味著如果這兩個實體都顯示在您的串列中,它們看起來是一樣的。因此,如果您使用的是data class,那么使用就足夠了,oldItem == newItem因為資料類具有equals比較每個屬性的函式。
在您的 Item 回呼代碼中,看起來您areItemsTheSame是正確的,但您areContentsTheSame過于復雜。由于Item是一個資料類,你只需要直接比較這兩個專案即可。
override fun areContentsTheSame(oldItem: Item, newItem: Item) = oldItem == newItem
在您的 Group 回呼代碼中,如果這是確定專案相同的有效方法,也許您可??以比較舊專案和新專案的 GroupKey。由于您僅使用直接==比較,因此當專案部分更改時,您可能會出現一些視覺缺陷,例如視圖消失和重新出現,而不是簡單地更改一些文本。
問題 2
您應該很少需要===在 Kotlin 中使用。它不僅檢查兩個專案是否等效,而且檢查這兩個專案是否參考記憶體中完全相同的實體。它根本不適合 DiffUtil.ItemCallback。
問題 3
==是比較任意兩個物件的正確方法。在 Kotlin 中,甚至原語也應該以這種方式進行比較,因為它們的行為類似于物件。
問題 5
使用 ListAdapter,您應該始終使用submitList代替notifyDataSetChanged。notifyDataSetChanged將導致所有視圖的無意義重繪 并破壞使用 ListAdapter 和 DiffUtil 的目的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/330653.html
