如何检查TabLayout中是否已删除Tab?

时间:2016-06-18 13:12:49

标签: android android-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);

     }
    }
   }

1 个答案:

答案 0 :(得分:0)

我在我的应用中使用它:

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
}

你能尝试再来吗?