优化目录界面

pull/1693/head
kunfei 3 years ago
parent 001c9355bf
commit 81558bdc90
  1. 20
      app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt
  3. 17
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt

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

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

@ -22,6 +22,7 @@ import io.legado.app.utils.ColorUtils
import io.legado.app.utils.observeEvent import io.legado.app.utils.observeEvent
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.Default
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -111,11 +112,23 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
} }
}.let { }.let {
adapter.setItems(it) 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)
}
} }
override fun clearDisplayTitle() { override fun clearDisplayTitle() {

Loading…
Cancel
Save