使用包含具有自己FragmentPagerAdapter的Fragment的顶级FragmentPagerAdapter进行挣扎

时间:2015-04-10 00:39:22

标签: android android-fragments fragmentpageradapter

拥有2级UI,顶级有一个由FragmentPagerAdapter驱动的标签条。其中一个片段又有一个第二级FragmentPagerAdapter和它自己的标签条,带有几个“子”片段。现在,子代码片段在第一次遍历顶级选项卡时显示。但是“子”片段在第二次迭代时不显示

以下是如何在顶层存在的一个片段中创建subtabpager适配器

公共最终类MainLevelTabFragment扩展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();
    }
}

1 个答案:

答案 0 :(得分:0)

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