我已经实现了带有片段的选项卡布局,并且一切正常。我唯一不喜欢的是选项卡中的 Web 视图(每次加载同一个站点时都会加载),每次我离开选项卡然后回到它时都会加载站点。有没有办法让 web 视图只加载一次?我真的不知道从哪里开始...
这是类 TabInfo(帮助类)的代码,第一次为所有选项卡调用的添加选项卡方法,以及选项卡更改事件处理程序
private class TabInfo {
private String tag;
private Class clss;
private Bundle args;
private Fragment fragment;
TabInfo(String tag, Class clazz, Bundle args) {
this.tag = tag;
this.clss = clazz;
this.args = args;
}
}
private void addTab(String tag, String title, TabInfo tabInfo) {
TabSpec ts = getTabSpec(tag, title);
// Attach a Tab view factory to the spec
ts.setContent(new TabFactory(this));
// String tag = tabSpec.getTag();
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
tabInfo.fragment = getSupportFragmentManager().findFragmentByTag(tag);
if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(tabInfo.fragment);
ft.commit();
getSupportFragmentManager().executePendingTransactions();
}
mTabHost.addTab(ts);
}
public void onTabChanged(String tag) {
try {
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();
}
// fillCommonData();
} catch (Exception e) {
LogHelper.WriteLogError("error in onTabChanged function", e);
}
}
谢谢