I have a ViewPager
that displays 10 fragments. However when I change the FragmentPagerAdapter
and set it to the ViewPager
only the not-currently-shown fragments are changed. I tried invalidating the ViewPager
, calling notifyDataSetChanged()
in the FragmentPagerAdapter
and this SO answer and only changing the data instead of the adapter(and this answer is using a PagerAdapter
instead of FragmentPagerAdapter
but without success.
This is how I initialize the ViewPager
viewPager = rootView.findViewById(R.id.view_pager)
changeData()
and then later for changing I call this function
fun changeData(params: List<CustomObject> = ArrayList()){
viewPager.adapter = CustomCPagerAdapter(fragmentManager!!, params)
}
and the CustomPagerAdapter
class CustomPagerAdapter(fm: FragmentManager,
private var customDataSet: List<CustomObject>)
: FragmentPagerAdapter(fm) {
override fun getItem(pos: Int): Fragment {
return CustomFragment.newInstance(customDataSet[pos])
}
override fun getCount(): Int {
return customDataSet.size
}
}
Why is the ViewPager
keeping the displayed fragments even though a new FragmentPagerAdapter
is added to it? How do I change this?