From 675aa0cb88850f7cdd9d44ced089c9bbb4f6ab97 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 7 Feb 2020 10:12:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/ToTopListUpCallback.kt | 34 +++++++++++++++++++ .../ui/main/bookshelf/books/BooksFragment.kt | 6 +++- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/io/legado/app/help/ToTopListUpCallback.kt diff --git a/app/src/main/java/io/legado/app/help/ToTopListUpCallback.kt b/app/src/main/java/io/legado/app/help/ToTopListUpCallback.kt new file mode 100644 index 000000000..6fdef3dca --- /dev/null +++ b/app/src/main/java/io/legado/app/help/ToTopListUpCallback.kt @@ -0,0 +1,34 @@ +package io.legado.app.help + +import androidx.recyclerview.widget.ListUpdateCallback +import androidx.recyclerview.widget.RecyclerView +import io.legado.app.base.adapter.ItemViewHolder + +class ToTopListUpCallback : ListUpdateCallback { + var firstInsert = -1 + lateinit var adapter: RecyclerView.Adapter + + override fun onChanged(position: Int, count: Int, payload: Any?) { + adapter.notifyItemRangeChanged(position, count, payload) + } + + override fun onMoved(fromPosition: Int, toPosition: Int) { + if (toPosition == 0) { + firstInsert = 0 + } + adapter.notifyItemMoved(fromPosition, toPosition) + } + + override fun onInserted(position: Int, count: Int) { + if (firstInsert == -1 || firstInsert > position) { + firstInsert = position + } + adapter.notifyItemRangeInserted(position, count) + } + + override fun onRemoved(position: Int, count: Int) { + adapter.notifyItemRangeRemoved(position, count) + } + + +} \ No newline at end of file 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 d7ffd7a34..496792c62 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 @@ -15,6 +15,7 @@ import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.data.entities.Book import io.legado.app.help.IntentDataHelp +import io.legado.app.help.ToTopListUpCallback import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.accentColor import io.legado.app.ui.audio.AudioPlayActivity @@ -88,6 +89,8 @@ class BooksFragment : BaseFragment(R.layout.fragment_books), else -> App.db.bookDao().observeByGroup(groupId) } bookshelfLiveData?.observe(this, Observer { + val listUpCallback = ToTopListUpCallback() + listUpCallback.adapter = booksAdapter val diffResult = DiffUtil.calculateDiff( BooksDiffCallBack( @@ -96,7 +99,8 @@ class BooksFragment : BaseFragment(R.layout.fragment_books), ) ) booksAdapter.setItems(it, false) - diffResult.dispatchUpdatesTo(booksAdapter) + diffResult.dispatchUpdatesTo(listUpCallback) + rv_bookshelf.scrollToPosition(listUpCallback.firstInsert) }) }