Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Sep 8, 2024
1 parent 4cb69f6 commit 230b6e8
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,21 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
kotlin.runCatching {
val oldItems = this.items.toList()
val itemsSize = items?.size ?: 0
val headerCount = getHeaderCount()
val footerCount = getFooterCount()
val callback = object : DiffUtil.Callback() {
override fun getOldListSize(): Int {
return itemCount
}

override fun getNewListSize(): Int {
return (items?.size ?: 0) + getHeaderCount() + getFooterCount()
return itemsSize + headerCount + footerCount
}

override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldItems.getOrNull(oldItemPosition - getHeaderCount())
val oldItem = oldItems.getOrNull(oldItemPosition - headerCount)
?: return true
val newItem = items?.getOrNull(newItemPosition - getHeaderCount())
val newItem = items?.getOrNull(newItemPosition - headerCount)
?: return true
return itemCallback.areItemsTheSame(oldItem, newItem)
}
Expand All @@ -136,17 +138,17 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
oldItemPosition: Int,
newItemPosition: Int
): Boolean {
val oldItem = oldItems.getOrNull(oldItemPosition - getHeaderCount())
val oldItem = oldItems.getOrNull(oldItemPosition - headerCount)
?: return true
val newItem = items?.getOrNull(newItemPosition - getHeaderCount())
val newItem = items?.getOrNull(newItemPosition - headerCount)
?: return true
return itemCallback.areContentsTheSame(oldItem, newItem)
}

override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
val oldItem = oldItems.getOrNull(oldItemPosition - getHeaderCount())
val oldItem = oldItems.getOrNull(oldItemPosition - headerCount)
?: return null
val newItem = items?.getOrNull(newItemPosition - getHeaderCount())
val newItem = items?.getOrNull(newItemPosition - headerCount)
?: return null
return itemCallback.getChangePayload(oldItem, newItem)
}
Expand Down

0 comments on commit 230b6e8

Please sign in to comment.