How to initialize tab content when app is initialized
I have an app that has 2 tabs. Each tab loads an xml file (fairly large, maybe 400 item rss file).
By default the tab doesn't get the xml until it's clicked on. I simply wanted a way to load it all when the app is first opened.
Here is the main view:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Audio Feed
intent = new Intent().setClass(this, AudioFeed.class);
spec = tabHost.newTabSpec("audio").setIndicator("",
res.getDrawable(R.drawable.ic_tab_audio))
.setContent(intent);
tabHost.addTab(spec);
// Video Feed
i开发者_StackOverflow社区ntent = new Intent().setClass(this, VideoFeed.class);
spec = tabHost.newTabSpec("video").setIndicator("",
res.getDrawable(R.drawable.ic_tab_video))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0); //todo: remember what tab user was last on
}
I use TabHost.setCurrentTab(x) or TabHost.setCurrentTabByTag(x).
mTabHost.setCurrentTab(1);
mTabHost.setCurrentTab(0);
For Initialization I use TabHost.OnTabChangeListener.
private OnTabChangeListener mOnTabChangeListener = new OnTabChangeListener() {
@Override
public void onTabChanged(String tag) {
if (FBIntent.EXTRA_XX.equals(tag)) {
// Current tab is xx.
... if xx not init -> ...
} else if (FBIntent.EXTRA_YY.equals(tag)) {
// Current tab is yy.
...
}
}
};
In this same method do whats the other tabs do. I mean what the other do when you click them. So you have the same thing for all tabs. Don't forget to include this same tab in the OnTabChanged()
method and do the same operations again, otherwise you'll get nothing when this tab is clicked again.
精彩评论