diff --git a/app/src/main/java/io/legado/app/ui/book/toc/TocActivity.kt b/app/src/main/java/io/legado/app/ui/book/toc/TocActivity.kt index 7152219a0..e9138501d 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/TocActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/TocActivity.kt @@ -1,3 +1,5 @@ +@file:Suppress("DEPRECATION") + package io.legado.app.ui.book.toc import android.content.Intent @@ -8,9 +10,8 @@ import androidx.activity.viewModels import androidx.appcompat.widget.SearchView import androidx.core.view.isGone import androidx.fragment.app.Fragment -import androidx.viewpager2.adapter.FragmentStateAdapter +import androidx.fragment.app.FragmentPagerAdapter import com.google.android.material.tabs.TabLayout -import com.google.android.material.tabs.TabLayoutMediator import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.databinding.ActivityChapterListBinding @@ -37,12 +38,7 @@ class TocActivity : VMBaseActivity() { tabLayout.isTabIndicatorFullWidth = false tabLayout.setSelectedTabIndicatorColor(accentColor) binding.viewPager.adapter = TabFragmentPageAdapter() - TabLayoutMediator(tabLayout, binding.viewPager) { tab, position -> - when (position) { - 0 -> tab.setText(R.string.chapter_list) - else -> tab.setText(R.string.bookmark) - } - }.attach() + tabLayout.setupWithViewPager(binding.viewPager) intent.getStringExtra("bookUrl")?.let { viewModel.initBook(it) } @@ -98,16 +94,25 @@ class TocActivity : VMBaseActivity() { } } - private inner class TabFragmentPageAdapter : FragmentStateAdapter(this) { + @Suppress("DEPRECATION") + private inner class TabFragmentPageAdapter : + FragmentPagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { + + override fun getItem(position: Int): Fragment { + return when (position) { + 1 -> BookmarkFragment() + else -> ChapterListFragment() + } + } - override fun getItemCount(): Int { + override fun getCount(): Int { return 2 } - override fun createFragment(position: Int): Fragment { + override fun getPageTitle(position: Int): CharSequence { return when (position) { - 0 -> ChapterListFragment() - else -> BookmarkFragment() + 1 -> getString(R.string.bookmark) + else -> getString(R.string.chapter_list) } } diff --git a/app/src/main/res/layout/activity_chapter_list.xml b/app/src/main/res/layout/activity_chapter_list.xml index ff03b765e..c78973583 100644 --- a/app/src/main/res/layout/activity_chapter_list.xml +++ b/app/src/main/res/layout/activity_chapter_list.xml @@ -12,7 +12,7 @@ app:contentInsetStartWithNavigation="0dp" app:contentLayout="@layout/view_tab_layout" /> -