pull/1059/head
gedoor 4 years ago
parent fcdc167d3f
commit 13bf96c475
  1. 31
      app/src/main/java/io/legado/app/ui/book/toc/TocActivity.kt
  2. 2
      app/src/main/res/layout/activity_chapter_list.xml

@ -1,3 +1,5 @@
@file:Suppress("DEPRECATION")
package io.legado.app.ui.book.toc package io.legado.app.ui.book.toc
import android.content.Intent import android.content.Intent
@ -8,9 +10,8 @@ import androidx.activity.viewModels
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.core.view.isGone import androidx.core.view.isGone
import androidx.fragment.app.Fragment 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.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.databinding.ActivityChapterListBinding import io.legado.app.databinding.ActivityChapterListBinding
@ -37,12 +38,7 @@ class TocActivity : VMBaseActivity<ActivityChapterListBinding, TocViewModel>() {
tabLayout.isTabIndicatorFullWidth = false tabLayout.isTabIndicatorFullWidth = false
tabLayout.setSelectedTabIndicatorColor(accentColor) tabLayout.setSelectedTabIndicatorColor(accentColor)
binding.viewPager.adapter = TabFragmentPageAdapter() binding.viewPager.adapter = TabFragmentPageAdapter()
TabLayoutMediator(tabLayout, binding.viewPager) { tab, position -> tabLayout.setupWithViewPager(binding.viewPager)
when (position) {
0 -> tab.setText(R.string.chapter_list)
else -> tab.setText(R.string.bookmark)
}
}.attach()
intent.getStringExtra("bookUrl")?.let { intent.getStringExtra("bookUrl")?.let {
viewModel.initBook(it) viewModel.initBook(it)
} }
@ -98,16 +94,25 @@ class TocActivity : VMBaseActivity<ActivityChapterListBinding, TocViewModel>() {
} }
} }
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 return 2
} }
override fun createFragment(position: Int): Fragment { override fun getPageTitle(position: Int): CharSequence {
return when (position) { return when (position) {
0 -> ChapterListFragment() 1 -> getString(R.string.bookmark)
else -> BookmarkFragment() else -> getString(R.string.chapter_list)
} }
} }

@ -12,7 +12,7 @@
app:contentInsetStartWithNavigation="0dp" app:contentInsetStartWithNavigation="0dp"
app:contentLayout="@layout/view_tab_layout" /> app:contentLayout="@layout/view_tab_layout" />
<androidx.viewpager2.widget.ViewPager2 <androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager" android:id="@+id/view_pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />

Loading…
Cancel
Save