ViewPager inside Fragment loses content when returning to it

最后都变了- 提交于 2019-12-04 03:35:39

if you are using your ViewPager in a Fragment, when you are creating a new adapter, you should use ChildFragmentManager:

getChildFragmentManager()

Try this:

    ViewPager pager = [your viewPAger]
    pager.setOffscreenPageLimit([quantity of tabs that you are going to use]);

with that sentence (pager.setOffscreenPageLimit) you can specify how far away a tab can stand before it get erased by the ViewPager!

Cheers!

Use an AsyncTask to set the ViewPagerAdapter:

private class SetAdapterTask extends AsyncTask<Void, Void, Void> {
        protected Void doInBackground(Void... params) {
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            if(mPagerAdapter != null) mViewPager.setAdapter(mPagerAdapter);
        }
    }   

Call it like this:

mAdapter = new PageAdapter(getChildFragmentManager(), fragments);
new SetAdapterTask().execute();

instead of calling:

mViewPager.setAdapter(mPagerAdapter);

And set the Adapter inside the onCreateView(...) method, not onActivityCreated(...).

Also, use getChildFragmentManager() instead of getFragmentManager().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!