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 trouble with is implementing a add and delete command that the user will be able to use to delete a tab if desired or add a tab with a url of their choice that works just like the others.
Below is my code.
Here is the main java file that all other files use:
public class UniversityofColorado extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost host=getTabHost();
host.addTab(host.newTabSpec("one")
.setIndicator("Google")
.setContent(new Intent(this, Hello.class)));
host.addTab(host.newTabSpec("two")
.setIndicator("Colorado Main Site")
.setContent(new Intent(this, ColoradoMainSiteBrowser.class)));
host.addTab(host.newTabSpec("three")
.setIndicator("CULearn")
.setContent(new Intent(this, CULearnBrowser.class)));
host.addTab(host.newTabSpec("four")
.setIndicator("CULink")
.setContent(new Intent(this, CULinkBrowser.class)));
host.addTab(host.newTabSpec("five")
.setIndicator("MyCUInfo")
.setContent(new Intent(this, MyCUInfoBrowser.class)));
host.addTab(host.newTabSpec("six")
.setIndicator("Campus Map")
.setContent(new Intent(this, CampusBrowser.clas开发者_JAVA技巧s)));
host.addTab(host.newTabSpec("Seven")
.setIndicator("Notes")
.setContent(new Intent(this, Notepadv3.class)));
}
// Inflates menu when "menu Key" is pressed
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
}
Then I have each webpage defined in a separate java file that the main file calls below is one of them:
public class ColoradoMainSiteBrowser extends Activity {
WebView webview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://colorado.edu/");
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I have the menu defined in the main file I just need to construct the methods so the buttons do what they are suppose to do. Any help would be great.
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.
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<TabHost.TabSpec> list = new ArrayList<TabHost.TabSpec>();
// 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.
精彩评论