如何检查 Tab 是否已在 TabLayout 中删除?
How to check if a Tab is already removed in TabLayout?
在 MainActivity 下,这就是我添加 TABS 的方式
tab1 = tabLayout.newTab().setText("TAB 1");
tabLayout.addTab(tab1);
tab2 = (tabLayout.newTab().setText("TAB 2"));
tabLayout.addTab(tab2);
所以,基本上我已经从对话框中实现了我的程序来添加和删除选项卡,但我的问题是我无法找到一种方法来检查选项卡是否已被删除因此,如果选项卡,我的程序就会出错之前已经删除
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK || data == null)
return;
settingList = (ArrayList < SettingCheckBox > ) data.getSerializableExtra(SETTING_CHECK_BOX);
for (int i = 0; i < settingList.size(); i++) {
if (settingList.get(i).getChecked()) {
Log.d("**Checked Item**", String.valueOf(settingList.get(i).getDescription()));
//gets the description of the tab that is to be removed
String removeTab = String.valueOf(settingList.get(i).getDescription());
//gets the value of the check box
Boolean checkedValue = settingList.get(i).getChecked();
if (removeTab.equals("TAB 1")) {
//How to check if Tab is already removed ?
if (checkedvalue)
if (tabLayout.equals(tab1)) {
tabLayout.removeTab(tab1);
//basically removes fragment associated with the tab
pagerAdapter.removeTab(0, tab1);
}
}
} else if (removeTab.equals("TAB 2")) {
if (checkedValue) {
tabLayout.removeTab(tab2);
pagerAdapter.removeTab(1, tab2);
}
}
}
我在我的应用程序中使用这个:
public boolean isMatching(final String tabString){
boolean match=false;
for(int i=0;i<tablayout.getTabCount();i++){
if(tablayout.getTabAt(i).getText().toString().equals(tabString)){
match=true;
}
}
return match;
}
然后使用:
if(isMatching(yourstring)){
//you didnt removed it,
// there is at least one tab that equals your string
}else{
//there is no tab that matches your string
}
你能试试看再来吗?
在 MainActivity 下,这就是我添加 TABS 的方式
tab1 = tabLayout.newTab().setText("TAB 1");
tabLayout.addTab(tab1);
tab2 = (tabLayout.newTab().setText("TAB 2"));
tabLayout.addTab(tab2);
所以,基本上我已经从对话框中实现了我的程序来添加和删除选项卡,但我的问题是我无法找到一种方法来检查选项卡是否已被删除因此,如果选项卡,我的程序就会出错之前已经删除
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK || data == null)
return;
settingList = (ArrayList < SettingCheckBox > ) data.getSerializableExtra(SETTING_CHECK_BOX);
for (int i = 0; i < settingList.size(); i++) {
if (settingList.get(i).getChecked()) {
Log.d("**Checked Item**", String.valueOf(settingList.get(i).getDescription()));
//gets the description of the tab that is to be removed
String removeTab = String.valueOf(settingList.get(i).getDescription());
//gets the value of the check box
Boolean checkedValue = settingList.get(i).getChecked();
if (removeTab.equals("TAB 1")) {
//How to check if Tab is already removed ?
if (checkedvalue)
if (tabLayout.equals(tab1)) {
tabLayout.removeTab(tab1);
//basically removes fragment associated with the tab
pagerAdapter.removeTab(0, tab1);
}
}
} else if (removeTab.equals("TAB 2")) {
if (checkedValue) {
tabLayout.removeTab(tab2);
pagerAdapter.removeTab(1, tab2);
}
}
}
我在我的应用程序中使用这个:
public boolean isMatching(final String tabString){
boolean match=false;
for(int i=0;i<tablayout.getTabCount();i++){
if(tablayout.getTabAt(i).getText().toString().equals(tabString)){
match=true;
}
}
return match;
}
然后使用:
if(isMatching(yourstring)){
//you didnt removed it,
// there is at least one tab that equals your string
}else{
//there is no tab that matches your string
}
你能试试看再来吗?