如何不在选项卡 activity 上显示某些选项卡?

how not to show some tabs on tabbed activity?

我有以下 activity 显示不同的选项卡。

public class BagContent extends FragmentActivity implements ActionBar.TabListener, CosmeListener {

String[] profileTypes;

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {

    setContentView(R.layout.activity_bag_content);
    mViewPager = (ViewPager) findViewById(R.id.pager);
    final ActionBar actionBar = getActionBar();
    actionBar.setTitle(getString(R.string.title_activity_bag_content) + " [" + bagName + "]");
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
    }
}

@Override
protected void onResume() {
    super.onResume();
    // TODO Auto-generated method stub
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            return new FormatShortFragment();
        case 1:
            return new FormatLongFragment();
        case 2:
            return new FormatPlantFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_bag_content_format_short).toUpperCase(l);
        case 1:
            return getString(R.string.title_bag_content_format_long).toUpperCase(l);
        case 2:
            return getString(R.string.title_bag_content_format_plant).toUpperCase(l);
        }
        return null;
    }
}

我有不同的配置文件类型,根据每种类型,我必须显示一些选项卡或其他选项卡。

实际上,每种配置文件类型的所有选项卡都已显示。我尝试手动插入操作栏上的选项卡,但随后选项卡未正确显示片段。另一个问题是当我转到右边缘不存在的选项卡时导航中断。

我需要这方面的帮助,如何才能 select 仅针对特定个人资料类型的 1 个选项卡?

例如,我如何才能只显示 title_bag_content_format_long 和他的 profileTypes[1] 片段?

任何走动都可能很好,例如从某些配置文件类型或任何其他内容中删除一些选项卡。

非常感谢大家,我真的需要帮助。

已编辑

我做了一些修改:

向 SectionsPagerAdapter 构造函数添加了配置文件类型参数:

private String profileType;
public SectionsPagerAdapter(FragmentManager fm, String profileType) {
    super(fm);
    this.profileType = profileType;
}

然后我修改了其他方法来控制配置文件类型:

    @Override
    public Fragment getItem(int position) {
        if (this.profileType.equals(profileTypes[1])){
            switch (position) {
            case 0:
                return new FormatPlantFragment();
            }
        } else {
            switch (position) {
            case 0:
                return new FormatShortFragment();
            case 1:
                return new FormatLongFragment();
            case 2:
                return new FormatPlantFragment();
            }
        }

        return null;
    }

    @Override
    public int getCount() {
        if (this.profileType.equals(profileTypes[1])){
            return 1;
        }
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        if (this.profileType.equals(profileTypes[1])){
            switch (position) {
                case 0:
                    return getString(R.string.title_bag_content_format_plant).toUpperCase(l);
            }
        } else {
            switch (position) {
            case 0:
                return getString(R.string.title_bag_content_format_short).toUpperCase(l);
            case 1:
                return getString(R.string.title_bag_content_format_long).toUpperCase(l);
            case 2:
                return getString(R.string.title_bag_content_format_plant).toUpperCase(l);
            }
        }
        return null;
    }

这工作正常,并且按配置文件类型进行过滤器更加通用。 感谢 Filpe.costa01 给了我所需的推动力。

您正在 return 浏览所有选项卡,因为您的方法

@Override
public int getCount() {
    // Show 3 total pages.
    return 3;
}

return 3 个标签页。也许您可以使用 SectionsPagerAdapter 构造函数来包含您想要的选项卡数量,具体取决于所需的每个配置文件。

像这样:

private mNumOfTabs;
public SectionsPagerAdapter(FragmentManager fm, int numberOfTabs) {
    super(fm);
    mNumOfTabs = numberOfTabs;
}

你应该为这个替换你的 getCount() 方法:

@Override
public int getCount() {
    // Show 3 total pages.
    return mNumOfTabs;
}

以上升的方式组织您的个人资料(低调=少量标签),您可以像现在一样使用这种方法

@Override
public Fragment getItem(int position) {
    switch (position) {
    case 0:
        return new FormatShortFragment();
    case 1:
        return new FormatLongFragment();
    case 2:
        return new FormatPlantFragment();
    }

    return null;
}

否则,您应该考虑配置文件并实施新逻辑来为每个配置文件检索正确的选项卡。