如何在我的Activity中删除tablayout中的选项卡?

时间:2016-12-28 10:09:45

标签: java android tabs android-tablayout

我的TabLayout中有3个标签,我的问题是,例如,我想删除或禁用TabLayoutViewPager中的一个或多个特定标签,是否可以实现,我该怎么做?

MainActivity

public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener{

TabLayout tabLayout;
public static ViewPager viewPager;
DBController db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    tabLayout = (TabLayout) findViewById(R.id.tabLayout);

    tabLayout.removeViewAt(3);

    viewPager = (ViewPager) findViewById(R.id.pager);


    Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());

    viewPager.setAdapter(adapter);

    tabLayout.setOnTabSelectedListener(this);

    viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager()));
    tabLayout.setupWithViewPager(viewPager);


}

@Override
public void onTabSelected(TabLayout.Tab tab) {
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;

}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.cartlist) {
        Intent intent = new Intent(this,cart_list.class);
        startActivity(intent);
        return true;
    }
        return true;
}
public class SectionPagerAdapter extends FragmentPagerAdapter {

    public SectionPagerAdapter(FragmentManager fm) {
        super(fm);

    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
                case 0:
                    return new Tab1();
                case 1:
                     return new Tab2();
                case 2:
                    return new Tab4();
            }
        return  null;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "ALL";
            case 1:
                return "WESTERN";
            case 2:
                return "-";
        }
        return null;
    }
}

2 个答案:

答案 0 :(得分:1)

您可以使用以下FragmentPagerAdapter实施:

public class BasePagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();
    public BasePagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }
    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }

    @Override
    public CharSequence removeFragment(int position) {
        mFragmentTitleList.remove(position);
        mFragmentList.remove(position);
        notifyDatasetChanged();
    }

}

添加标签:

adapter.addFragment(new Tab1(), "All");
adapter.addFragment(new Tab2(), "Western");

删除标签:

adapter.removeFragment(1);

编辑:适配器初始化:

tabLayout = (TabLayout) findViewById(R.id.tabLayout);
viewPager = (ViewPager) findViewById(R.id.pager);

BasePagerAdapter adapter = new BasePagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1(), "All");
adapter.addFragment(new Tab2(), "Western");
adapter.addFragment(new Tab3(), "-");

viewPager.setAdapter(adapter);
tabLayout.setOnTabSelectedListener(this);
tabLayout.setupWithViewPager(viewPager);

答案 1 :(得分:1)

您必须根据自己的情况动态返回getCount()getPageTitle()