TabFragment 中的 ViewPager 未再次加载

ViewPager in TabFragment is not loading a second time(TabFragment 中的 ViewPager 未再次加载)

本文介绍了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 未再次加载