2016-06-18 85 views
0

在MainActivity这是怎么了,我因此增加TABS如何检查TabLayout中的Tab是否已被删除?

 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); 

    } 
    } 
    } 

回答

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 
} 

你可以尝试喜欢就回来?

+0

是的工作。感谢名单。顺便问一下,你知道哪些地方可以了解更多信息吗? – choman

+1

@choman是的,材料指南签出此网站:[标签布局指南](https://material.google.com/components/tabs.html#tabs-specs),了解tablayout,参数,功能等:[ TabLayout](https://developer.android.com/reference/android/support/design/widget/TabLayout.html)。你也可以查找代码路径指南:[Codepath Tablayout](https://github.com/codepath/android_guides/wiki/Google-Play-Style-Tabs-using-TabLayout) –

+0

非常感谢Yasin。 :) – choman