Reputation: 1608
There are a lot of similar question but none of them solved my problem.
I got nested ViewPager
s and nested FragmentStatePagerAdapter
s in my application.
In details, a first ViewPager (backed with a 4 fragments adapter) is created, the first fragment of which contains other 3 fragments.
My problem is that, based on some events, the middle Fragment is not showing at all.
As soon as the app is started, everything works as intended and is showing correctly.
If I switch page on the parent ViewPager, the behavior is different based on the page: if I go from the first (which contains the nested ViewPager) to the second and then back to the first, still everything works as expected; If I go to any other page and then back to the first one, at first the ViewPager goes fully transparent but is still swipeable. In order to make the view get redrawed on the screen I have to swipe a couple of times from 1st to 3rd page, but no matter what, the 2nd Fragment is not showing. It is fully trasparent.
I've already read a lots of similar question, but couldn't find any working fix. I'm very confused at the moment, I've tried everything.
Here's my code for reference:
Parent Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
mCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.feed_coordinator);
mFloatingActionButton = (FloatingActionButton) findViewById(R.id.feed_floating_upload);
mViewPager = (ViewPager) findViewById(R.id.feed_view_pager);
mTabLayout = (TabLayout) findViewById(R.id.feed_tab_layout);
setupViewPager(); // this sets adapter to viewpager
mTabLayout.setupWithViewPager(mViewPager);
setupTabLayout();
}
private class ParentPagerAdapter extends FragmentStatePagerAdapter {
public ParentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Frag1();
case 1:
return new Frag2();
case 2:
return new Frag3();
case 3:
return new Frag4();
default:
return new Default();
}
}
@Override
public int getCount() {
return 4;
}
}
Frag1 is pretty much similar:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag1, container, false);
[...]
mViewPager = (ViewPager) view.findViewById(R.id....);
mLinearLayout = (LinearLayout) view.findViewById(R.id....);
mFragmentAdapter = new MyFragmentAdapter(getFragmentManager());
mViewPager.setAdapter(mFragmentAdapter);
return view;
}
private class MyFragmentAdapter extends FragmentStatePagerAdapter {
private static final int ITEMS = 3;
public MyFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
new Child1();
case 1:
new Child2();
case 2:
new Child3();
default:
return null;
}
}
@Override
public int getCount() {
return ITEMS;
}
}
The 3 childs follow a very simple implementation, they fetch data from server and show them in a RecyclerView. If needed I will post the class too.
I really don't know what to do, nothing seems to work. Any kind of help would be really appreciated, thanks!
Upvotes: 2
Views: 1661
Reputation: 1768
For nested Fragments, you will have to use getChildFragmentManager()
.
Change this line :
new MyFragmentAdapter(getFragmentManager());
To :
new MyFragmentAdapter(getChildFragmentManager());
Upvotes: 7