I'm using viewpager 2 now.
The views of the fragments are all the same, so I'm using the same fragments.
And the data is shown through recyclerview in the fragment.
All fragments set different data list.
OnResume retrieves new data through a network call and performs recyclerviewAdapter.submitlist.
It shows the same data view during swiping.
If onResume does not update the data, it shows a different view during swiping.
class CommunityViewPagerAdapter(
fragmentManager: FragmentManager,
lifecycle: Lifecycle,
) : FragmentStateAdapter(fragmentManager, lifecycle) {
private val fragments = listOf(
TechCommunityFragment.newInstance("Backend"),
TechCommunityFragment.newInstance("Frontend"),
TechCommunityFragment.newInstance("Android"),
TechCommunityFragment.newInstance("IOS"),
TechCommunityFragment.newInstance("DataScience"),
TechCommunityFragment.newInstance("DataAnalysis"),
TechCommunityFragment.newInstance("Algorithm"),
TechCommunityFragment.newInstance("DataStructure"),
TechCommunityFragment.newInstance("Network"),
TechCommunityFragment.newInstance("OperatingSystem"),
)
override fun getItemCount(): Int {
return fragments.size
}
override fun createFragment(position: Int): Fragment {
return fragments[position]
}
}
private fun initView() = with(binding) {
viewPager.adapter= CommunityViewPagerAdapter(childFragmentManager, lifecycle)
TabLayoutMediator(techTablayout, viewPager) { tab, position ->
tab.text = mainTabArray[position]
}.attach()
}
override fun onResume() {
super.onResume()
techStack = arguments?.getString(TECH_STACK) ?: ""
callAPI()
}