|
|
@@ -12,6 +12,7 @@ import androidx.lifecycle.repeatOnLifecycle
|
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
+import androidx.recyclerview.widget.RecyclerView.Adapter.StateRestorationPolicy
|
|
|
import io.legado.app.R
|
|
|
import io.legado.app.base.BaseFragment
|
|
|
import io.legado.app.constant.AppLog
|
|
|
@@ -72,7 +73,6 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
|
|
|
}
|
|
|
}
|
|
|
private var booksFlowJob: Job? = null
|
|
|
- private var savedInstanceState: Bundle? = null
|
|
|
var position = 0
|
|
|
private set
|
|
|
var groupId = -1L
|
|
|
@@ -83,7 +83,6 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
|
|
|
private var enableRefresh = true
|
|
|
|
|
|
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
- this.savedInstanceState = savedInstanceState
|
|
|
arguments?.let {
|
|
|
position = it.getInt("position", 0)
|
|
|
groupId = it.getLong("groupId", -1)
|
|
|
@@ -113,11 +112,12 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
|
|
|
} else {
|
|
|
binding.rvBookshelf.setRecycledViewPool(activityViewModel.booksGridRecycledViewPool)
|
|
|
}
|
|
|
+ booksAdapter.stateRestorationPolicy = StateRestorationPolicy.PREVENT_WHEN_EMPTY
|
|
|
binding.rvBookshelf.adapter = booksAdapter
|
|
|
booksAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
|
|
|
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
|
|
|
val layoutManager = binding.rvBookshelf.layoutManager
|
|
|
- if (positionStart == 0 && layoutManager is LinearLayoutManager) {
|
|
|
+ if (positionStart == 0 && itemCount == 1 && layoutManager is LinearLayoutManager) {
|
|
|
val scrollTo = layoutManager.findFirstVisibleItemPosition() - itemCount
|
|
|
binding.rvBookshelf.scrollToPosition(max(0, scrollTo))
|
|
|
}
|
|
|
@@ -125,7 +125,7 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
|
|
|
|
|
|
override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
|
|
|
val layoutManager = binding.rvBookshelf.layoutManager
|
|
|
- if (toPosition == 0 && layoutManager is LinearLayoutManager) {
|
|
|
+ if (toPosition == 0 && itemCount == 1 && layoutManager is LinearLayoutManager) {
|
|
|
val scrollTo = layoutManager.findFirstVisibleItemPosition() - itemCount
|
|
|
binding.rvBookshelf.scrollToPosition(max(0, scrollTo))
|
|
|
}
|
|
|
@@ -191,25 +191,11 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
|
|
|
binding.tvEmptyMsg.isGone = list.isNotEmpty()
|
|
|
binding.refreshLayout.isEnabled = enableRefresh && list.isNotEmpty()
|
|
|
booksAdapter.setItems(list)
|
|
|
- recoverPositionState()
|
|
|
delay(100)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private fun recoverPositionState() {
|
|
|
- // 恢复书架位置状态
|
|
|
- if (savedInstanceState?.getBoolean("needRecoverState") == true) {
|
|
|
- val layoutManager = binding.rvBookshelf.layoutManager
|
|
|
- if (layoutManager is LinearLayoutManager) {
|
|
|
- val leavePosition = savedInstanceState!!.getInt("leavePosition")
|
|
|
- val leaveOffset = savedInstanceState!!.getInt("leaveOffset")
|
|
|
- layoutManager.scrollToPositionWithOffset(leavePosition, leaveOffset)
|
|
|
- }
|
|
|
- savedInstanceState!!.putBoolean("needRecoverState", false)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
private fun startLastUpdateTimeJob() {
|
|
|
upLastUpdateTimeJob?.cancel()
|
|
|
if (!AppConfig.showLastUpdateTime) {
|
|
|
@@ -241,28 +227,6 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
|
|
|
return booksAdapter.itemCount
|
|
|
}
|
|
|
|
|
|
- override fun onSaveInstanceState(outState: Bundle) {
|
|
|
- super.onSaveInstanceState(outState)
|
|
|
- // 保存书架位置状态
|
|
|
- val layoutManager = binding.rvBookshelf.layoutManager
|
|
|
- if (layoutManager is LinearLayoutManager) {
|
|
|
- val itemPosition = layoutManager.findFirstVisibleItemPosition()
|
|
|
- val currentView = layoutManager.findViewByPosition(itemPosition)
|
|
|
- val viewOffset = currentView?.top
|
|
|
- if (viewOffset != null) {
|
|
|
- outState.putInt("leavePosition", itemPosition)
|
|
|
- outState.putInt("leaveOffset", viewOffset)
|
|
|
- outState.putBoolean("needRecoverState", true)
|
|
|
- } else if (savedInstanceState != null) {
|
|
|
- val leavePosition = savedInstanceState!!.getInt("leavePosition")
|
|
|
- val leaveOffset = savedInstanceState!!.getInt("leaveOffset")
|
|
|
- outState.putInt("leavePosition", leavePosition)
|
|
|
- outState.putInt("leaveOffset", leaveOffset)
|
|
|
- outState.putBoolean("needRecoverState", true)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
override fun onDestroyView() {
|
|
|
super.onDestroyView()
|
|
|
/**
|