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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在我的Activity中删除tablayout中的选项卡?相关的知识,希望对你有一定的参考价值。

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

主要信息

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

您可以使用以下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);
另一答案

你必须根据你的情况动态地返回qazxsw poi和qazxsw poi。

以上是关于如何在我的Activity中删除tablayout中的选项卡?的主要内容,如果未能解决你的问题,请参考以下文章

Tablayout 和 Viewpager 之间的小填充

如何在不单击的情况下显示片段

如何将tablayout的指示器从底部更改为顶部?

使用ViewPager和TabLayout与Xamarin时出错

如何在 Android 中以编程方式创建 TabLayout

如何使用 TabLayout 在 Fragment 中使用 CardView 行实现 RecyclerView