问题描述
我试图做的是类似的东西。我即使使用相同的布局:
I am trying to do something similar as http://stackoverflow.com/a/24437224/2277631. I am even using the same layout:
我,因为我用的是NavigationView(每个选项是一个片段),有第一个选项与选项卡片段(所以ViewPager更多碎片)以下的这种方法。
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(新适配器视图(getChildFragmentManager()));
基本上,使用ViewPager为嵌套的片段,只加载1页(从3个选项卡),当我刷他们(每个标签是它ContentLoader的片段)只加载其他页面。我发现,使用 setOffscreenPageLimit
被在这种情况下被忽略。相反,如果我使用: getActivity()。getSupportFragmentManager()
则ViewPager工作正常(缺省1页面加载和下和previous页)。但后来,很多怪异的东西发生(因为我想使用嵌套的片段时,使用 getChildFragmentManager
)。所以......用ViewPager嵌套的片段,让 setOffscreenPageLimit
的任何想法来工作?
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 setOffscreenPageLimit
gets 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?
修改1(15年12月30日)
跟踪bug被纠正了,我查了所有的片段被创建。问题是,这三个片段有他们自己的 LoaderCallback
,但只有第一个调用 onLoadFinished
。试图找出为什么其他片段只能调用导航到它(在工作没有嵌套的方式)。
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).
推荐答案
我遇到了同样的问题。这是我该怎么办,这是为我工作,它具有相同的architetcure如你所期望的有。
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());
}
这篇关于与getChildFragmentManager使用它作为NestedFragment时ViewPager setOffscreenPageLimit不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!