与包含带有自己的 FragmentPagerAdapter 的片段的顶级 FragmentPagerAdapter 作斗争

Struggling with a top level FragmentPagerAdapter containing a Fragment with its own FragmentPagerAdapter

有一个 2 层 UI,顶层有一个由 FragmentPagerAdapter 提供支持的标签条。其中一个片段依次有一个二级 FragmentPagerAdapter 和它自己的带有几个 "sub" 片段的标签条。现在,子片段在第一次遍历顶级选项卡时显示。但是 "sub" 片段不会在第二次迭代时显示

下面是如何在存在于顶层的片段之一中创建 subtabpager 适配器

public final class MainLevelTabFragment extends Fragment {

private SubpageTabsPagerAdapter mSubpagePagerAdapter;
private ViewPager mSubpageViewPager;
private SlidingTabLayout mSlidingTabLayout;


@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {

    View rootView = null;
    Bundle bundle = getArguments();

        rootView = inflater.inflate(R.layout.fragment_tab_main_level, container, false);
        mSubpageViewPager = (ViewPager) rootView.findViewById(R.id.subpage_screen_tabs_pager);
        mSlidingTabLayout = (SlidingTabLayout) rootView.findViewById(R.id.sliding_tabs);

        mSubpagePagerAdapter = new SubpageTabsPagerAdapter(getSupportFragmentManager, getActivity());
        mSubpageViewPager.setAdapter(mSubpagePagerAdapter);

    return rootView;
}

SubpageTabsPagerAdapter 代码如下所示

public class SubpageTabsPagerAdapter extends FragmentPagerAdapter {

    private Context mContext;
    private Cursor mCursor;

    public SubpageTabsPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        mContext = context;
    }

    @Override
    public Fragment getItem(int position) {
        if (null == mCursor)
            return null;

        mCursor.moveToPosition(position);

        Fragment fragment = new SubtabFragment();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public int getCount() {
        return (null != mCursor) ? mCursor.getCount() : 0;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (null == mCursor)
            return null;

        mCursor.moveToPosition(position);

        return "title";
    }

    public void swapCursor(Cursor c) {
        if (mCursor == c)
            return;

        mCursor = c;
        notifyDataSetChanged();
    }
}

我认为您可能必须在 SubpageTabsPagerAdapter 构造函数中使用 getChildFragmentManager() 而不是 getSupportFragmentManager()