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" /> -