diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt index 509e514ba..6853a1f07 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt @@ -8,13 +8,13 @@ import android.widget.LinearLayout import androidx.lifecycle.LiveData import androidx.lifecycle.Observer import androidx.recyclerview.widget.DividerItemDecoration -import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseFragment import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.lib.theme.backgroundColor +import io.legado.app.ui.widget.recycler.UpLinearLayoutManager import io.legado.app.utils.getViewModelOfActivity import kotlinx.android.synthetic.main.fragment_chapter_list.* import org.jetbrains.anko.sdk27.listeners.onClick @@ -28,6 +28,7 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme lateinit var adapter: ChapterListAdapter private var chapterData: LiveData>? = null private var durChapterIndex = 0 + private lateinit var mLayoutManager: UpLinearLayoutManager override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) @@ -38,7 +39,8 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme private fun initRecyclerView() { adapter = ChapterListAdapter(requireContext(), this) - recycler_view.layoutManager = LinearLayoutManager(requireContext()) + mLayoutManager = UpLinearLayoutManager(requireContext()) + recycler_view.layoutManager = mLayoutManager recycler_view.addItemDecoration( DividerItemDecoration( requireContext(), @@ -55,7 +57,7 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme viewModel.book?.let { book -> durChapterIndex = book.durChapterIndex tv_current_chapter_info.text = book.durChapterTitle - recycler_view.scrollToPosition(durChapterIndex) + mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0) } }) } @@ -63,15 +65,15 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme private fun initView() { ll_chapter_base_info.setBackgroundColor(backgroundColor) - iv_chapter_top.onClick { recycler_view.scrollToPosition(0) } + iv_chapter_top.onClick { mLayoutManager.scrollToPositionWithOffset(0, 0) } iv_chapter_bottom.onClick { if (adapter.itemCount > 0) { - recycler_view.scrollToPosition(adapter.itemCount - 1) + mLayoutManager.scrollToPositionWithOffset(adapter.itemCount - 1, 0) } } tv_current_chapter_info.onClick { viewModel.book?.let { - recycler_view.scrollToPosition(it.durChapterIndex) + mLayoutManager.scrollToPositionWithOffset(it.durChapterIndex, 0) } } viewModel.callBack = this @@ -86,7 +88,7 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme chapterData?.observe(viewLifecycleOwner, Observer { adapter.setItems(it) }) - recycler_view.scrollToPosition(0) + mLayoutManager.scrollToPositionWithOffset(0, 0) } } diff --git a/app/src/main/java/io/legado/app/ui/widget/recycler/UpLinearLayoutManager.kt b/app/src/main/java/io/legado/app/ui/widget/recycler/UpLinearLayoutManager.kt new file mode 100644 index 000000000..0df53aba7 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/widget/recycler/UpLinearLayoutManager.kt @@ -0,0 +1,43 @@ +package io.legado.app.ui.widget.recycler + +import android.content.Context +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.LinearSmoothScroller + +class UpLinearLayoutManager: LinearLayoutManager { + val context: Context + + constructor(context: Context) : super(context) { + this.context = context + } + + fun smoothScrollToPosition(position: Int) { + smoothScrollToPosition(position, 0) + } + + fun smoothScrollToPosition(position: Int, offset: Int) { + val scroller = UpLinearSmoothScroller(context) + scroller.targetPosition = position + scroller.offset = offset + startSmoothScroll(scroller) + } +} + +class UpLinearSmoothScroller(context: Context?): LinearSmoothScroller(context) { + var offset = 0 + + override fun getVerticalSnapPreference(): Int { + return SNAP_TO_START + } + + override fun getHorizontalSnapPreference(): Int { + return SNAP_TO_START + } + + override fun calculateDtToFit(viewStart: Int, viewEnd: Int, boxStart: Int, boxEnd: Int, snapPreference: Int): Int { + if (snapPreference == SNAP_TO_START) { + return boxStart - viewStart + offset + } + throw IllegalArgumentException("snap preference should be SNAP_TO_START") + } +} \ No newline at end of file