2

I am trying to do something similar as https://stackoverflow.com/a/24437224/2277631. I am even using the same layout:

enter image description here

I am following this approach because I am using a NavigationView (each option is a fragment) that has the first option as a fragment with Tabs (so a ViewPager with more fragments).

I found a problem when using:

viewPager.setAdapter(new AdapterView(getChildFragmentManager()));

Basically, using the ViewPager as nested fragment, it only loads 1 page (from the 3 tabs), and it only loads the other pages when I swipe to them (each tab is a fragment with its ContentLoader). I found that using setOffscreenPageLimitgets ignored in this case. Instead, if I use: getActivity().getSupportFragmentManager() then the ViewPager works as expected (loading by default 1 page and the next and previous pages). But then, a lot of weird stuff happen (cause I am suppose to use getChildFragmentManager when using nested fragments). So... Any ideas of using ViewPager as nested fragment and make setOffscreenPageLimit to work?

Edit 1 (30 Dec 15)

Tracking the bug down, I checked that all the fragments are been created. The problem is that the three fragments have they own LoaderCallback but only the first one calls onLoadFinished. Trying to figure out why the other fragments only call onLoadFinished when navigating to it (was working without the nested approach).

Community
  • 1
  • 1
Ferran Negre
  • 3,453
  • 3
  • 32
  • 55

1 Answers1

0

I ran into the same problem. This is how I do and this is working for me, it has the same architetcure as what you expect to have.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        view = inflater.inflate(R.layout.communitylayout, container, false);
        Bundle data = getArguments();

        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.comtabs);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);


        // add tabs
        tabLayout.addTab(tabLayout.newTab());
        tabLayout.addTab(tabLayout.newTab());
        tabLayout.addTab(tabLayout.newTab());

        RelativeLayout layout1 = (RelativeLayout) inflater.inflate(R.layout.communitytablayoutleft, container, false);
        RelativeLayout layout2 = (RelativeLayout) inflater.inflate(R.layout.communitytablayout, container, false);
        RelativeLayout layout3 = (RelativeLayout) inflater.inflate(R.layout.communitytablayoutright, container, false);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);



        pager = (ViewPager) view.findViewById(R.id.compager);
        CommunityPagerFragment adapter = new CommunityPagerFragment(getChildFragmentManager());

        pager.setAdapter(adapter);
        tabLayout.setupWithViewPager(pager);
        pager.setCurrentItem(0);
        tabLayout.setOnTabSelectedListener(this);

        // KEEP FRAGMENT STATE INSIDE OF THE TAB
        pager.setOffscreenPageLimit(2);


        ((TextView)layout1.findViewById(R.id.tabtext)).setText(tabs[0]);
        ((TextView)layout2.findViewById(R.id.tabtext)).setText(tabs[1]);
        ((TextView)layout3.findViewById(R.id.tabtext)).setText(tabs[2]);

        //tabLayout.set


        tabLayout.getTabAt(0).setCustomView(layout1);
        tabLayout.getTabAt(1).setCustomView(layout2);
        tabLayout.getTabAt(2).setCustomView(layout3);

        onTabSelected(tabLayout.getTabAt(1));
        //tabLayout.set

        return view;

And if you want to get the Tabs working by click in addition of the swipe, add the following:

@Override
public void onTabSelected(TabLayout.Tab tab) {

    pager.setCurrentItem(tab.getPosition());

}
Virthuss
  • 2,665
  • 1
  • 15
  • 36
  • Mmmm so you just recommend not to use nested fragments in this case? I have 3 fragments where I send a bundle and depending on those arguments they load something using a ContentLoader. I am not really sure your solution would help here. I think I won't use nested fragments, place the ViewPager in the main activity and hide/show it depending on the selected option :/ – Ferran Negre Dec 29 '15 at 23:20