How to allow user to Add and Delete Tabs in an android application

前端 未结 2 806

I am developing an application that uses tabs with each tab being linked to a webpage that the user will be able to see and interact with using webview. What I am having tro

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-14 20:18

    Using code mentioned by Mohammed can be useful, but you may need to add

    tabs.setCurrentTab(0);
    

    before calling

    tabs.clearAllTabs();
    

    according to issue described at http://code.google.com/p/android/issues/detail?id=2772. Then your tab probably gets removed, but you may notice an error when trying to switch or add tabs. For me, this problem was fixed after calling

    tabs.setCurrentTab(index);
    

    inside the for-loop (after adding the tab).

    So you should get:

    list.remove(nTabToRemoveIndex);
    tabs.setCurrentTab(0);   // <== ***FIRST EDIT***
    tabs.clearAllTabs();
    int nTabIndex = 0;       // <== ***SECOND EDIT***
    for(TabHost.TabSpec spec : list)
    {
      tabs.addTab(spec);
      tabs.setCurrentTab(nTabIndex++);   // <== ***THIRD EDIT***
    }
    

    Hope it will help.

提交回复
热议问题