getChildFragmentManager () cannot be resolved or cannot be referenced

I had exactly same issue and I managed to resolve it by simply adding:

viewpager.setOffscreenPageLimit(2);

As was suggested in this post.


you should do like:

adapter = new ViewPagerAdapter(this.getChildFragmentManager());

getChildFragmentManager() is a method of a Fragment. Since you are subclassing AppCompatActivity, you can't access it. If you are using the Fragment from the support library you can use getSupportFragmentManager()


This is probably not the most elegant solution, but in my case I needed the viewpager to be running in its own fragment, while having the toolbar and tablayout reside in the MainAcitivy. Only drawback is that you have to hide the TabLayout manually in other Fragments which not use the Tab/Viewpager.

But this way you can still call getChildFragmentManager from the Fragment.

@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_rides_host, container, false);

    viewPager = (ViewPager) v.findViewById(R.id.fm_feed_host_viewpager);

    setupViewPager(viewPager);

    ((MainActivity)getActivity()).mTabLayout.setupWithViewPager(viewPager);

    return v;
}

private void setupViewPager(ViewPager viewPager) {

    ViewPagerAdapter adapter = new ViewPagerAdapter(this.getChildFragmentManager());

    adapter.addFragment(new YourFragment(), "Title");
    adapter.addFragment(...);
    viewPager.setAdapter(adapter);
}