TabFragment中的ViewPager不会再次加载 [英] ViewPager in TabFragment is not loading a second time

查看:62
本文介绍了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不会再次加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆