Sharas
Sharas

Reputation: 2135

Replace fragment in tablayout

I have a parent fragment containing TabLayout and ViewPager. The TabLayout has 3 tabs (fragments) managed by FragmentStatePagerAdapter.

After the user clicks on a button on the 3 fragment, I would like to replace it with different fragment, without reloading the parent fragment.

I tried to remove the fragment and add the new one and call notifyDataSetChanged() and the fragment is indeed replaced, but the TabLayout custom view headers are gone...

Any idea how can I do it right?

Thanks.

Upvotes: 0

Views: 242

Answers (1)

Juan
Juan

Reputation: 5589

You can have the 3rd Fragment working as a holder for two child Fragments which you can switch based on your app's logic (clicking the button you mention).

The key for this alternative is that the 3rd Fragment in the ViewPager will be the parent of the two Fragments that will be switching.

The two child Fragments will communicate with the parent Fragment for the parent to take care of doing the switching.

For the switching the parent will use a ChildFragmentManager instead of the FragmentManager used for Fragments that are managed by Activities.

Upvotes: 1

Related Questions