diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksDiffCallBack.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksDiffCallBack.kt index 9f7afd8bb..40ceecf40 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksDiffCallBack.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksDiffCallBack.kt @@ -7,11 +7,6 @@ import io.legado.app.data.entities.Book class BooksDiffCallBack(private val oldItems: List, private val newItems: List) : DiffUtil.Callback() { - - override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { - return oldItems[oldItemPosition].bookUrl == newItems[newItemPosition].bookUrl - } - override fun getOldListSize(): Int { return oldItems.size } @@ -20,6 +15,10 @@ class BooksDiffCallBack(private val oldItems: List, private val newItems: return newItems.size } + override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { + return oldItems[oldItemPosition].bookUrl == newItems[newItemPosition].bookUrl + } + override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { val oldItem = oldItems[oldItemPosition] val newItem = newItems[newItemPosition] diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt index 3c9f99b21..cc62f2cb1 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt @@ -104,13 +104,8 @@ class BooksFragment : BaseFragment(R.layout.fragment_books), else -> App.db.bookDao().observeByGroup(groupId) } bookshelfLiveData?.observe(this, Observer { - val diffResult = - DiffUtil.calculateDiff( - BooksDiffCallBack( - booksAdapter.getItems(), - it - ) - ) + val diffResult = DiffUtil + .calculateDiff(BooksDiffCallBack(ArrayList(booksAdapter.getItems()), it)) booksAdapter.setItems(it, false) diffResult.dispatchUpdatesTo(booksAdapter) }) diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreDiffCallBack.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreDiffCallBack.kt new file mode 100644 index 000000000..7f3c7d020 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreDiffCallBack.kt @@ -0,0 +1,34 @@ +package io.legado.app.ui.main.explore + +import androidx.recyclerview.widget.DiffUtil +import io.legado.app.data.entities.BookSource + + +class ExploreDiffCallBack( + private val oldItems: List, + private val newItems: List +) : + DiffUtil.Callback() { + + override fun getOldListSize(): Int { + return oldItems.size + } + + override fun getNewListSize(): Int { + return newItems.size + } + + override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { + return true + } + + override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { + val oldItem = oldItems[oldItemPosition] + val newItem = newItems[newItemPosition] + if (oldItem.bookSourceName != newItem.bookSourceName) { + return false + } + return true + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt index ea23c0909..80fb39212 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt @@ -5,7 +5,9 @@ import android.view.View import androidx.appcompat.widget.SearchView import androidx.lifecycle.LiveData import androidx.lifecycle.Observer +import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseFragment @@ -61,6 +63,15 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_find_ rv_find.layoutManager = linearLayoutManager adapter = ExploreAdapter(requireContext(), this, this) rv_find.adapter = adapter + adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() { + + override fun onItemRangeInserted(positionStart: Int, itemCount: Int) { + super.onItemRangeInserted(positionStart, itemCount) + if (positionStart == 0) { + rv_find.scrollToPosition(0) + } + } + }) } private fun initData(key: String? = null) { @@ -71,7 +82,10 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_find_ App.db.bookSourceDao().liveExplore("%$key%") } liveExplore?.observe(viewLifecycleOwner, Observer { + val diffResult = DiffUtil + .calculateDiff(ExploreDiffCallBack(ArrayList(adapter.getItems()), it)) adapter.setItems(it) + diffResult.dispatchUpdatesTo(adapter) }) }