From 99efc92484de014fb0af386182be07c6f93fc81c Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 15 Dec 2020 15:43:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=88=97=E8=A1=A8=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/ui/book/search/DiffCallBack.kt | 23 ++++--------------- .../app/ui/book/search/SearchAdapter.kt | 4 ++++ 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt b/app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt index 4125489b8..7cd271c19 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.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, private val newItems: List) : - DiffUtil.Callback() { +class DiffCallBack : DiffUtil.ItemCallback() { - 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, 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, 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) diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt index 10cb49d8d..20636f42f 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt @@ -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(context) { + override val diffItemCallback: DiffUtil.ItemCallback + get() = DiffCallBack() + override fun getViewBinding(parent: ViewGroup): ItemSearchBinding { return ItemSearchBinding.inflate(inflater, parent, false) }