diff --git a/app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt b/app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt index 12cc150f839c..bd4610e9309a 100644 --- a/app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt +++ b/app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt @@ -115,19 +115,21 @@ abstract class RecyclerAdapter(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) } @@ -136,17 +138,17 @@ abstract class RecyclerAdapter(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) }