kunfei 4 лет назад
Родитель
Сommit
da6a761a25

+ 16 - 4
app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt

@@ -5,19 +5,19 @@ import android.content.Intent
 import android.os.Bundle
 import android.view.View
 import androidx.fragment.app.activityViewModels
-import androidx.recyclerview.widget.LinearLayoutManager
 import io.legado.app.R
 import io.legado.app.base.VMBaseFragment
 import io.legado.app.data.appDb
 import io.legado.app.data.entities.Bookmark
 import io.legado.app.databinding.FragmentBookmarkBinding
 import io.legado.app.lib.theme.primaryColor
+import io.legado.app.ui.widget.recycler.UpLinearLayoutManager
 import io.legado.app.ui.widget.recycler.VerticalDivider
 import io.legado.app.utils.setEdgeEffectColor
 import io.legado.app.utils.showDialogFragment
 import io.legado.app.utils.viewbindingdelegate.viewBinding
+import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.Dispatchers.IO
-import kotlinx.coroutines.delay
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
 
@@ -27,19 +27,22 @@ class BookmarkFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_bookmark
     TocViewModel.BookmarkCallBack {
     override val viewModel by activityViewModels<TocViewModel>()
     private val binding by viewBinding(FragmentBookmarkBinding::bind)
+    private val mLayoutManager by lazy { UpLinearLayoutManager(requireContext()) }
     private val adapter by lazy { BookmarkAdapter(requireContext(), this) }
+    private var durChapterIndex = 0
 
     override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
         viewModel.bookMarkCallBack = this
         initRecyclerView()
         viewModel.bookData.observe(this) {
+            durChapterIndex = it.durChapterIndex
             upBookmark(null)
         }
     }
 
     private fun initRecyclerView() {
         binding.recyclerView.setEdgeEffectColor(primaryColor)
-        binding.recyclerView.layoutManager = LinearLayoutManager(requireContext())
+        binding.recyclerView.layoutManager = mLayoutManager
         binding.recyclerView.addItemDecoration(VerticalDivider(requireContext()))
         binding.recyclerView.adapter = adapter
     }
@@ -54,7 +57,16 @@ class BookmarkFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_bookmark
                 }
             }.let {
                 adapter.setItems(it)
-                delay(100)
+                var scrollPos = 0
+                withContext(Dispatchers.Default) {
+                    adapter.getItems().forEachIndexed { index, bookmark ->
+                        if (bookmark.chapterIndex >= durChapterIndex) {
+                            return@withContext
+                        }
+                        scrollPos = index
+                    }
+                }
+                mLayoutManager.scrollToPositionWithOffset(scrollPos, 0)
             }
         }
     }

+ 2 - 0
app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt

@@ -63,6 +63,7 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
     override fun onCurrentListChanged() {
         super.onCurrentListChanged()
         upDisplayTile()
+        callback.onListChanged()
     }
 
     fun upDisplayTile() {
@@ -157,6 +158,7 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
         val isLocalBook: Boolean
         fun openChapter(bookChapter: BookChapter)
         fun durChapterIndex(): Int
+        fun onListChanged()
     }
 
 }

+ 15 - 2
app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt

@@ -22,6 +22,7 @@ import io.legado.app.utils.ColorUtils
 import io.legado.app.utils.observeEvent
 import io.legado.app.utils.viewbindingdelegate.viewBinding
 import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers.Default
 import kotlinx.coroutines.Dispatchers.IO
 import kotlinx.coroutines.Dispatchers.Main
 import kotlinx.coroutines.launch
@@ -111,10 +112,22 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
                 }
             }.let {
                 adapter.setItems(it)
-                if (searchKey.isNullOrBlank() && mLayoutManager.findFirstVisibleItemPosition() < 0) {
-                    mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
+            }
+        }
+    }
+
+    override fun onListChanged() {
+        launch {
+            var scrollPos = 0
+            withContext(Default) {
+                adapter.getItems().forEachIndexed { index, bookChapter ->
+                    if (bookChapter.index >= durChapterIndex) {
+                        return@withContext
+                    }
+                    scrollPos = index
                 }
             }
+            mLayoutManager.scrollToPositionWithOffset(scrollPos, 0)
         }
     }