2016-06-18 87 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