ViewPager in TabFragment is not loading a second time(TabFragment 中的 ViewPager 未再次加载)
问题描述
我正在尝试制作一个在属于 TabHost
的片段中具有 ViewPager
的应用.一切正常.我有我的标签栏,我可以切换标签.当我使用 ViewPager
切换到选项卡时,一切都正确显示.
I'm trying to make an app that has a ViewPager
in a Fragment that is part of a TabHost
. Everything works out fine. I have my tabbar, I can switch tabs. When I switch to the tab with the ViewPager
, all is shown correctly.
但是,一旦我离开此标签并使用 ViewPager
并返回此标签,我的内容就不会显示.如果我滚动到一边两次,我确实会看到我的下一张图片,如果我返回两次,我还会看到图片已加载(可能是 offscreenloaded
).
However, as soon as I leave this tab with the ViewPager
and return this tab, my content is not shown. If I scroll to the side twice I do see my next image and if I go back two times I also see the images are loaded (probably the offscreenloaded
).
看到我的 TabFragment
正在重新实例化,但 ViewPager
中的片段却没有.
See that my TabFragment
is being reinstantiated when I return to it but the fragments in the ViewPager
aren't.
@Override
public void onActivityCreated(Bundle savedInstanceState) {
mProjectText = (TextView) getView().findViewById(R.id.projectText);
mProjectText.setText(mActiveProject.getInspirationText());
mAdapter = new AlbumAdapter(getFragmentManager(), mActiveProject.getInspiration());
mPager = (ViewPager)getView().findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
super.onActivityCreated(savedInstanceState);
}
public class AlbumAdapter extends FragmentStatePagerAdapter {
private ArrayList<ProjectContent> mItems;
public AlbumAdapter(FragmentManager fm, ArrayList<ProjectContent> items) {
super(fm);
this.mItems = items;
}
@Override
public Fragment getItem(int position) {
return AlbumContentFragment.newInstance(mItems.get(position));
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}}
推荐答案
我发现了问题.我花了两天时间,但嘿,它已经修好了.
I found the problem. It took me two days, but hey, it's fixed.
而不是使用
mAdapter = new AlbumAdapter(getFragmentManager(), mActiveProject.getInspiration());
你应该使用
mAdapter = new AlbumAdapter(getChildFragmentManager(), mActiveProject.getInspiration());
5 个字符就这么多.
这篇关于TabFragment 中的 ViewPager 未再次加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:TabFragment 中的 ViewPager 未再次加载
- GPS状态的广播接收器? 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- URL编码Swift iOS 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- UITextView 内容插图 2022-01-01