pull/1059/head
gedoor 4 years ago
parent 13bf96c475
commit 8282386f62
  1. 47
      app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt
  2. 2
      app/src/main/res/layout/activity_rss_artivles.xml

@ -1,3 +1,5 @@
@file:Suppress("DEPRECATION")
package io.legado.app.ui.rss.article package io.legado.app.ui.rss.article
import android.content.Intent import android.content.Intent
@ -7,8 +9,7 @@ import android.view.MenuItem
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter import androidx.fragment.app.FragmentStatePagerAdapter
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.ActivityRssArtivlesBinding import io.legado.app.databinding.ActivityRssArtivlesBinding
@ -20,9 +21,8 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
override val viewModel: RssSortViewModel override val viewModel: RssSortViewModel
by viewModels() by viewModels()
private var sorts = linkedMapOf<String, String>()
private lateinit var adapter: TabFragmentPageAdapter private lateinit var adapter: TabFragmentPageAdapter
private val fragmentMap = hashMapOf<Long, Fragment>() private val fragments = linkedMapOf<String, Fragment>()
private val upSourceResult = registerForActivityResult( private val upSourceResult = registerForActivityResult(
ActivityResultContracts.StartActivityForResult() ActivityResultContracts.StartActivityForResult()
) { ) {
@ -40,9 +40,7 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
adapter = TabFragmentPageAdapter() adapter = TabFragmentPageAdapter()
binding.viewPager.adapter = adapter binding.viewPager.adapter = adapter
TabLayoutMediator(binding.tabLayout, binding.viewPager) { tab, position -> binding.tabLayout.setupWithViewPager(binding.viewPager)
tab.text = sorts.keys.elementAt(position)
}.attach()
viewModel.titleLiveData.observe(this, { viewModel.titleLiveData.observe(this, {
binding.titleBar.title = it binding.titleBar.title = it
}) })
@ -78,10 +76,11 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
} }
private fun upFragments() { private fun upFragments() {
viewModel.rssSource?.sortUrls()?.let { fragments.clear()
sorts = it viewModel.rssSource?.sortUrls()?.forEach {
fragments[it.key] = RssArticlesFragment.create(it.key, it.value)
} }
if (sorts.size == 1) { if (fragments.size == 1) {
binding.tabLayout.gone() binding.tabLayout.gone()
} else { } else {
binding.tabLayout.visible() binding.tabLayout.visible()
@ -89,31 +88,25 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
adapter.notifyDataSetChanged() adapter.notifyDataSetChanged()
} }
private inner class TabFragmentPageAdapter : FragmentStateAdapter(this) { private inner class TabFragmentPageAdapter :
FragmentStatePagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
override fun getItemCount(): Int { override fun getItemPosition(`object`: Any): Int {
return sorts.size return POSITION_NONE
} }
override fun getItemId(position: Int): Long { override fun getPageTitle(position: Int): CharSequence {
val style = viewModel.rssSource?.articleStyle ?: 0 return fragments.keys.elementAt(position)
return style * 100 + super.getItemId(position)
} }
override fun containsItem(itemId: Long): Boolean { override fun getItem(position: Int): Fragment {
return fragmentMap.containsKey(itemId) return fragments.values.elementAt(position)
} }
override fun createFragment(position: Int): Fragment { override fun getCount(): Int {
val itemId = getItemId(position) return fragments.size
val fragment = RssArticlesFragment.create(
sorts.keys.elementAt(position),
sorts.values.elementAt(position)
)
fragmentMap[itemId] = fragment
return fragment
} }
} }
} }

@ -16,7 +16,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:tabMode="scrollable" /> app:tabMode="scrollable" />
<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="wrap_content" /> android:layout_height="wrap_content" />

Loading…
Cancel
Save