From e4882100cf6e39153980b9dcf6965a1a61a71b88 Mon Sep 17 00:00:00 2001 From: Administrator Date: Sat, 11 Jan 2020 17:33:56 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99LinearLayoutManager=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B9=A6=E7=B1=8D=E7=9B=AE=E5=BD=95=E6=A8=A1?= =?UTF-8?q?=E7=B3=8A=E6=90=9C=E7=B4=A2=E5=90=8EscrollToPosition=E5=9C=A8?= =?UTF-8?q?=E5=8F=AF=E8=A7=81=E8=8C=83=E5=9B=B4=E4=B8=8D=E7=BD=AE=E9=A1=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/chapterlist/ChapterListFragment.kt | 16 ++++--- .../widget/recycler/UpLinearLayoutManager.kt | 43 +++++++++++++++++++ 2 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/widget/recycler/UpLinearLayoutManager.kt 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