Replace fragment with another fragment inside ViewPager

后端 未结 5 620
生来不讨喜
生来不讨喜 2020-12-01 03:36

I\'m using a ViewPager together with a FragmentPagerAdapter to host three different fragments

[Fragment1][Fragment2][Fragment3]

What I\'m t

5条回答
  •  鱼传尺愫
    2020-12-01 04:21

    Like Maurycy said, you need to call mAdapter.notifyDataSetChanged() whenever you want to tell the ViewPager that the fragments have been replaced. However, you also need to override the getItemPosition() abstract function in your adapter and return POSITION_NONE when called with an old, to be hidden, fragment as argument. Otherwise, ViewPager will retain all current fragments.

    For more information and example code, see this answer to a related question.

提交回复
热议问题