从 TabLayout 添加和删除选项卡
Add and Remove Tabs from TabLayout
伙计们,我的布局中有三个选项卡,在另一个 activity 中,我有一个多选对话框,用于 select 我希望在我的布局中可见或不可见的选项卡,但我无法理解如何从我的布局中删除和添加选项卡和片段
public class TabAdapter extends FragmentStatePagerAdapter {
private String[] tituloTabs = {"X","Y","Z"};
public TabAdapter(@NonNull FragmentManager fm) {
super(fm);
}
@NonNull
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position){
case 0 :
fragment = new XFragment();
break;
case 1 :
fragment = new YFragment();
break;
case 2:
fragment = new ZFragment();
break;
}
return fragment;
}
@Override
public int getCount() {
return tituloTabs.length;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return tituloTabs[position];
}
Activity代码
SlidingTabLayout slidingTabLayout = findViewById(R.id.stl_tabs);
viewPager = findViewById(R.id.vp_pagina);
viewPager.setPageTransformer(true,new ZoomOutPageTransformer());
//---------------------------Configurar Sliding Tab--------------------------------------
slidingTabLayout.setSelectedIndicatorColors(ContextCompat.getColor(this,R.color.colorPrimary));
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setBackgroundColor( ContextCompat.getColor( this, R.color.black ) );
//-----------------------------Configurar Adapter ----------------------------------------
TabAdapter tabAdapter = new TabAdapter(getSupportFragmentManager());
viewPager.setAdapter(tabAdapter);
slidingTabLayout.setViewPager(viewPager);
在选项卡布局中添加选项卡
TabLayout tabLayout = findViewById(R.id.stl_tabs);
tablayout.addTab(tabLayout.newTab().setText("Tab1"))
tablayout.addTab(tabLayout.newTab().setText("Tab2"))
tablayout.addTab(tabLayout.newTab().setText("Tab3"))
在 TabAdapter getItem switch case
Tab1 -> case0 片段
Tab2 -> case1 片段
Tab3 -> 案例 2 片段
我设法找到了我的问题的解决方案,用 ArrayList 替换了数组,并以这种方式实现了添加删除片段和标题的方法
public TabAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public void removeitem(int position){
mFragmentList.remove(position);
}
public void removetitle(int position){
mFragmentTitleList.remove(position);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}}
伙计们,我的布局中有三个选项卡,在另一个 activity 中,我有一个多选对话框,用于 select 我希望在我的布局中可见或不可见的选项卡,但我无法理解如何从我的布局中删除和添加选项卡和片段
public class TabAdapter extends FragmentStatePagerAdapter {
private String[] tituloTabs = {"X","Y","Z"};
public TabAdapter(@NonNull FragmentManager fm) {
super(fm);
}
@NonNull
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position){
case 0 :
fragment = new XFragment();
break;
case 1 :
fragment = new YFragment();
break;
case 2:
fragment = new ZFragment();
break;
}
return fragment;
}
@Override
public int getCount() {
return tituloTabs.length;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return tituloTabs[position];
}
Activity代码
SlidingTabLayout slidingTabLayout = findViewById(R.id.stl_tabs);
viewPager = findViewById(R.id.vp_pagina);
viewPager.setPageTransformer(true,new ZoomOutPageTransformer());
//---------------------------Configurar Sliding Tab--------------------------------------
slidingTabLayout.setSelectedIndicatorColors(ContextCompat.getColor(this,R.color.colorPrimary));
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setBackgroundColor( ContextCompat.getColor( this, R.color.black ) );
//-----------------------------Configurar Adapter ----------------------------------------
TabAdapter tabAdapter = new TabAdapter(getSupportFragmentManager());
viewPager.setAdapter(tabAdapter);
slidingTabLayout.setViewPager(viewPager);
在选项卡布局中添加选项卡
TabLayout tabLayout = findViewById(R.id.stl_tabs);
tablayout.addTab(tabLayout.newTab().setText("Tab1"))
tablayout.addTab(tabLayout.newTab().setText("Tab2"))
tablayout.addTab(tabLayout.newTab().setText("Tab3"))
在 TabAdapter getItem switch case
Tab1 -> case0 片段
Tab2 -> case1 片段
Tab3 -> 案例 2 片段
我设法找到了我的问题的解决方案,用 ArrayList 替换了数组,并以这种方式实现了添加删除片段和标题的方法
public TabAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public void removeitem(int position){
mFragmentList.remove(position);
}
public void removetitle(int position){
mFragmentTitleList.remove(position);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}}