I have a viewpager2 tied to a tablayout with many different fragments holding textviews, i don't want the user to select another tab if he hasn't completed some of the textviews on each fragment. So when a user select another tab i would show a message asking to complete the current fragment data. I tried to select the old tab when a user select another tab in the "onTabUnselected" method of the TabSelectedListener but without success. Here is the code, please look at the TODO comment where i would like to prevent fragment changing.
viewPager = view.findViewById(R.id.mypager)
TabLayoutMediator(tabLayout, viewPager, true, false,
TabLayoutMediator.TabConfigurationStrategy { tab: TabLayout.Tab, position: Int ->
tab.text = titles[position]
}).attach()
tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
val currFragment= fragmentSet[tab!!.position]
if (!currFragment.validateFields()) {
//TODO: don't change tab, remain in this tab something like tab.aborselectedtTransition
}
}
override fun onTabReselected(tab: TabLayout.Tab?) {
}
})
Can anybody help me please?
question from:
https://stackoverflow.com/questions/65925334/how-to-prevent-viewpager2-from-turning-page-when-a-tab-is-selected 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…