ViewPager在TabFragment不加载第二次 [英] ViewPager in TabFragment not loading second time

查看:202
本文介绍了ViewPager在TabFragment不加载第二次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个应用程序,有一个ViewPager的片段是TabHost的一部分。

I'm trying to make an app that has a ViewPager in a Fragment that is part of a TabHost.

一切工作顺利。我有我的TabBar,我可以切换标签。当我切换到与ViewPager的标签,一切都正确显示。但是,当我离开这个标签与ViewPager并返回此选项卡,我的内容不会显示。如果我滚动到一边两次我看到我的下一个图像。如果我回去两次我也看到了图像的加载(可能是offscreenloaded)看到我的TabFragment是,当我返回,但在ViewPager的片段不被重新实例。

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. But 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) See that my TabFragment is being reinstantiated when i return to it but the fragments in the ViewPager aren't.

我已经包含了屏幕样机和我的一些code。

I've included a screen mockup and some of my code.

有人能帮忙吗?

干杯!

@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个字符。

So much for 5 characters.

这篇关于ViewPager在TabFragment不加载第二次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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