I have a FragmentPagerAdapter
(tabFragmentAdapter
) with 4 tabs, or fragments.
Every fragment has a RecyclerView
with adapter
.
Ever holder
is a list of cards that has a remove button.
When I press the remove button, the card gets deleted from the current fragment:
adapter?.notifyDataSetChanged()
But the problem is that I want the card to be removed in every tab. But when I swipe to the right, it is still there in the second tab but not in the third and fourth tab.
When I override getItemPosition
and return POSITION_NONE
:
override fun getItemPosition(fragment: Any): Int {
// POSITION NONE, so that it reloads every time.
return POSITION_NONE
}
I have the same problem and I see a little visible screen yank.
When I swipe back from the fourth position to the first, the second tab is updated and displayed correctly.
One solution is to call:
tabFragmentAdapter?.notifyDataSetChanged()
But this makes the list to briefly disappear and be shown again. This is not a nice effect.
When I add addOnTabSelectedListener
, the App hangs up.
val tabs: TabLayout = findViewById(R.id.tabs)
tabs.setupWithViewPager(viewPager)
tabs.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
Log.d("","")
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
Log.d("","")
tabFragmentAdapter?.notifyDataSetChanged()
}
override fun onTabReselected(tab: TabLayout.Tab?) {
Log.d("","")
}
})
I tried this SO answer