Removing Tabs using FragmentPagerAdapter

感情迁移 提交于 2019-12-25 03:54:43

问题


I am trying to create an Activity to give the user the option to add and remove certain tabs based on an option selected by the user using a toggle button. So if I have 4 tabs I need 4 toggle buttons that hide or add each of those tabs. The tabs are shown using ViewPager and I use FragmentPagerAdpater to show the different Fragments for each tab on the screen. What would I need to do in order to show and hide these same 4 tabs? From my research so far I see that calling destroyItem and instantiateItem but i'm not sure how to use these. I have posted code below. Any advice or helpful links will be appreciated.

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;
}

来源:https://stackoverflow.com/questions/32776240/removing-tabs-using-fragmentpageradapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!