优化列表更新

pull/736/head
gedoor 4 years ago
parent 85bc488e65
commit 99efc92484
  1. 23
      app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt
  2. 4
      app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt

@ -4,20 +4,9 @@ import android.os.Bundle
import androidx.recyclerview.widget.DiffUtil
import io.legado.app.data.entities.SearchBook
class DiffCallBack(private val oldItems: List<SearchBook>, private val newItems: List<SearchBook>) :
DiffUtil.Callback() {
class DiffCallBack : DiffUtil.ItemCallback<SearchBook>() {
override fun getNewListSize(): Int {
return newItems.size
}
override fun getOldListSize(): Int {
return oldItems.size
}
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition]
override fun areItemsTheSame(oldItem: SearchBook, newItem: SearchBook): Boolean {
return when {
oldItem.name != newItem.name -> false
oldItem.author != newItem.author -> false
@ -25,9 +14,7 @@ class DiffCallBack(private val oldItems: List<SearchBook>, private val newItems:
}
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition]
override fun areContentsTheSame(oldItem: SearchBook, newItem: SearchBook): Boolean {
return when {
oldItem.origins.size != newItem.origins.size -> false
oldItem.coverUrl != newItem.coverUrl -> false
@ -38,10 +25,8 @@ class DiffCallBack(private val oldItems: List<SearchBook>, private val newItems:
}
}
override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
override fun getChangePayload(oldItem: SearchBook, newItem: SearchBook): Any? {
val payload = Bundle()
val newItem = newItems[newItemPosition]
val oldItem = oldItems[oldItemPosition]
if (oldItem.name != newItem.name) payload.putString("name", newItem.name)
if (oldItem.author != newItem.author) payload.putString("author", newItem.author)
if (oldItem.origins.size != newItem.origins.size)

@ -3,6 +3,7 @@ package io.legado.app.ui.book.search
import android.content.Context
import android.os.Bundle
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter
@ -15,6 +16,9 @@ import org.jetbrains.anko.sdk27.listeners.onClick
class SearchAdapter(context: Context, val callBack: CallBack) :
SimpleRecyclerAdapter<SearchBook, ItemSearchBinding>(context) {
override val diffItemCallback: DiffUtil.ItemCallback<SearchBook>
get() = DiffCallBack()
override fun getViewBinding(parent: ViewGroup): ItemSearchBinding {
return ItemSearchBinding.inflate(inflater, parent, false)
}

Loading…
Cancel
Save