From 737a2d4d723df9ba0e080cdb3fdff3e0749c5c60 Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 10 May 2021 15:36:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=BF=87=E6=9C=9F=E6=96=B9?= =?UTF-8?q?=E6=B3=95viewPager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/main/bookshelf/BookshelfFragment.kt | 37 +++++++++---------- .../main/res/layout/fragment_bookshelf.xml | 2 +- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt index 090edd420..116261ded 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt @@ -5,11 +5,14 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.View -import android.view.ViewGroup import androidx.appcompat.widget.SearchView -import androidx.fragment.app.* +import androidx.fragment.app.Fragment +import androidx.fragment.app.activityViewModels +import androidx.fragment.app.viewModels import androidx.lifecycle.LiveData +import androidx.viewpager2.adapter.FragmentStateAdapter import com.google.android.material.tabs.TabLayout +import com.google.android.material.tabs.TabLayoutMediator import io.legado.app.R import io.legado.app.base.VMBaseFragment import io.legado.app.constant.AppConst @@ -44,7 +47,7 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b private val binding by viewBinding(FragmentBookshelfBinding::bind) override val viewModel: BookshelfViewModel by viewModels() private val activityViewModel: MainViewModel by activityViewModels() - private lateinit var adapter: FragmentStatePagerAdapter + private lateinit var adapter: FragmentStateAdapter private lateinit var tabLayout: TabLayout private var bookGroupLiveData: LiveData>? = null private val bookGroups = mutableListOf() @@ -107,10 +110,12 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b tabLayout.isTabIndicatorFullWidth = false tabLayout.tabMode = TabLayout.MODE_SCROLLABLE tabLayout.setSelectedTabIndicatorColor(requireContext().accentColor) - tabLayout.setupWithViewPager(binding.viewPagerBookshelf) binding.viewPagerBookshelf.offscreenPageLimit = 1 - adapter = TabFragmentPageAdapter(childFragmentManager) + adapter = TabFragmentPageAdapter() binding.viewPagerBookshelf.adapter = adapter + TabLayoutMediator(tabLayout, binding.viewPagerBookshelf) { tab, i -> + tab.text = bookGroups[i].groupName + }.attach() } private fun initBookGroupData() { @@ -240,29 +245,21 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b }.show() } - private inner class TabFragmentPageAdapter(fm: FragmentManager) : - FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { + private inner class TabFragmentPageAdapter : + FragmentStateAdapter(this) { - override fun getPageTitle(position: Int): CharSequence { - return bookGroups[position].groupName - } - - override fun getItemPosition(`object`: Any): Int { - return POSITION_NONE - } - - override fun getItem(position: Int): Fragment { + override fun getItemId(position: Int): Long { val group = bookGroups[position] - return BooksFragment.newInstance(position, group.groupId) + return group.groupId } - override fun getCount(): Int { + override fun getItemCount(): Int { return bookGroups.size } - override fun instantiateItem(container: ViewGroup, position: Int): Any { - val fragment = super.instantiateItem(container, position) as BooksFragment + override fun createFragment(position: Int): Fragment { val group = bookGroups[position] + val fragment = BooksFragment.newInstance(position, group.groupId) fragmentMap[group.groupId] = fragment return fragment } diff --git a/app/src/main/res/layout/fragment_bookshelf.xml b/app/src/main/res/layout/fragment_bookshelf.xml index 4fbff35c3..0986b9bea 100644 --- a/app/src/main/res/layout/fragment_bookshelf.xml +++ b/app/src/main/res/layout/fragment_bookshelf.xml @@ -13,7 +13,7 @@ app:title="@string/bookshelf" app:contentLayout="@layout/view_tab_layout_min" /> -