使用FragmentPagerAdapter删除选项卡

时间:2015-09-25 06:39:24

标签: android tabs android-viewpager fragmentpageradapter

我正在尝试创建一个Activity,以便用户可以根据用户使用切换按钮选择的选项添加和删除某些选项卡。因此,如果我有4个标签,我需要4个切换按钮来隐藏或添加每个标签。使用ViewPager显示选项卡,我使用FragmentPagerAdpater显示屏幕上每个选项卡的不同片段。为了显示和隐藏这4个标签,我需要做什么?从我的研究到目前为止,我看到调用destroyItem和instantiateItem,但我不知道如何使用这些。我在下面发布了代码。任何建议或有用的链接将不胜感激。

class TabPageAdapter extends FragmentPagerAdapter {

    public TabPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {


    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case JudgeActivity.JUDGE_MAIN:
                if (mMainFragment == null) {
                    mMainFragment = JudgeMainFragment.newInstance();
                }
                return mMainFragment;
            case JudgeActivity.JUDGE_VERDICTS:
                if (mVerdictFragment == null) {
                    mVerdictFragment = JudgeVerdictFragment.newInstance();
                }
                return mVerdictFragment;
            case JudgeActivity.JUDGE_CLASSIFY:
                if (mClassifyFragment == null) {
                    mClassifyFragment = JudgeClassifyFragment.newInstance();
                }
                return mClassifyFragment;
            case JudgeActivity.JUDGE_SIDEBAR:
                if (mSidebarFragment == null) {
                    mSidebarFragment = JudgeSidebarFragment.newInstance((SidebarCall) mActivity);
                }
                return mSidebarFragment;
        }
        return null;
    }

    @Override
    public int getCount() {
        return CONTENT.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return CONTENT[position % CONTENT.length].toUpperCase(Locale.US);
    }
}


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mActivity = (JudgeActivity) getActivity();
    Context context = new ContextThemeWrapper(mActivity, R.style.Theme_JudgePageIndicatorDefaults);
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mView = inflater.inflate(R.layout.fragment_judge, container, false);

    FragmentPagerAdapter adapter = new TabPageAdapter(getChildFragmentManager());

    mViewPager = (ViewPager) mView.findViewById(R.id.pager);
    mViewPager.setOffscreenPageLimit(3);
    mViewPager.setAdapter(adapter);

    return mView;
}

0 个答案:

没有答案