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

前端 未结 2 808

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:15

    You may use this way: 1- add every tabs to list array 2-to remove a spesific tab, first remove it from list and clear all tabs from tabHost and add them again from list.

    check this code which has basic idea of how to delete tabs from tabHost dynamically :

    // data structure, what I referred to as memory
    ArrayList list = new ArrayList();
    
    // when you are adding tabs to tab host
    // what you add, you remember
    TabHost.TabSpec spec = tabs.newTabSpec("tag1");
    spec.setContent(R.id.button);
    spec.setIndicator("TabONe");
    tabs.addTab(spec);
    list.add(spec);
    ...
    // when you want to remove
    list.remove(list.size()-1); // remove it from memory
    tabs.clearAllTabs();  // clear all tabs from the tabhost
    for(TabHost.TabSpec spec : list) // add all that you remember back
       tabs.addTab(spec);
    

    resource: http://www.coderanch.com/t/460859/Android/Mobile/TabHost-Remove-Tab

    I hope this will help.

    M.

提交回复
热议问题