我是一名初学者 android 开发人员,我不明白为什么我的第一个标签内容没有显示。我已经在网上搜索了很长时间,但我还没有成功。:(
其他两个选项卡正确显示,只是第一个失败(尽管我附加了片段,所以我认为这不是第一个片段的问题)
我尝试仔细遵循一些教程并研究了许多示例,但我仍然找不到原因......
这是我设置tabHost的方法:
private void initialiseTabHost(Bundle args) {
mTabHost = (TabHost)findViewById(R.id.main_tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("My").setIndicator(createTabView(mTabHost.getContext(), "My")), ( tabInfo = new TabInfo("My", MyCinemasFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("All").setIndicator(createTabView(mTabHost.getContext(), "All")), ( tabInfo = new TabInfo("All", AllCinemasFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Win!").setIndicator(createTabView(mTabHost.getContext(), "Win!")), ( tabInfo = new TabInfo("Win!", OffersFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
this.onTabChanged("My");
mTabHost.setOnTabChangedListener(this);
}
这是我的 addTab 方法:
private static void addTab(TabsFragmentActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
tabSpec.setContent(activity.new TabFactory(activity));
String tag = tabSpec.getTag();
tabInfo.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
ft.detach(tabInfo.fragment);
ft.commit();
activity.getSupportFragmentManager().executePendingTransactions();
}
tabHost.addTab(tabSpec);
}
但是,我觉得 onTabChanged 把一切都搞砸了……
public void onTabChanged(String tag) {
TabInfo newTab = (TabInfo) this.mapTabInfo.get(tag);
if (mLastTab != newTab) {
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
if (mLastTab != null) {
if (mLastTab.fragment != null) {
ft.detach(mLastTab.fragment);
}
}
if (newTab != null) {
if (newTab.fragment == null) {
newTab.fragment = Fragment.instantiate(this, newTab.clss.getName(), newTab.args);
ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
} else {
ft.attach(newTab.fragment);
}
}
mLastTab = newTab;
ft.commit();
this.getSupportFragmentManager().executePendingTransactions();
}
}
先感谢您...