pull/94/head
kunfei 5 years ago
parent 96b3846313
commit 675aa0cb88
  1. 34
      app/src/main/java/io/legado/app/help/ToTopListUpCallback.kt
  2. 6
      app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.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<ItemViewHolder>
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)
}
}

@ -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)
})
}

Loading…
Cancel
Save