Daw*_*odo 15 android android-tabhost android-fragments
我使用的是多选项卡的FragmentTabHost(构造如图所示这里).但是,我无法使用getFragmentByTag(在这种情况下返回null)随机地查找我的选项卡,除非通过至少单击选项卡一次激活已解决的选项卡.
FragmentTabHost似乎延迟了标签的创建,直到我们确实需要它们(也就是用户点击它并想要查看它).
有没有办法强制主机立即创建它们,以便我可以通过getFragmentByTag安全地访问它们?
或者是否可以"自己创建"标签并将它们添加到TabHost?
有没有办法强制主机立即创建它们,以便我可以通过getFragmentByTag安全地访问它们?
不,因为交易是在执行的onAttachedToWindow().我们来看看源代码:
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
String currentTab = getCurrentTabTag();
// Go through all tabs and make sure their fragments match.
// the correct state.
FragmentTransaction ft = null;
for (int i=0; i<mTabs.size(); i++) {
TabInfo tab = mTabs.get(i);
tab.fragment = mFragmentManager.findFragmentByTag(tab.tag);
if (tab.fragment != null && !tab.fragment.isDetached()) {
if (tab.tag.equals(currentTab)) {
// The fragment for this tab is already there and
// active, and it is what we really want to have
// as the current tab. Nothing to do.
mLastTab = tab;
} else {
// This fragment was restored in the active state,
// but is not the current tab. Deactivate it.
if (ft == null) {
ft = mFragmentManager.beginTransaction();
}
ft.detach(tab.fragment);
}
}
}
// We are now ready to go. Make sure we are switched to the
// correct tab.
mAttached = true;
ft = doTabChanged(currentTab, ft);
if (ft != null) {
ft.commit();
mFragmentManager.executePendingTransactions();
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mAttached = false;
}
Run Code Online (Sandbox Code Playgroud)
如你所见,mFragmentManager.executePendingTransactions();执行于onAttachedToWindow.
或者是否可以"自己创建"标签并将它们添加到TabHost?
是的,您可以使用tabhost,您可以使用以下方法创建选项卡内容.
public TabHost.TabSpec setContent(int viewId)
public TabHost.TabSpec setContent(Intent intent)
public TabHost.TabSpec setContent(TabHost.TabContentFactory contentFactory)
| 归档时间: |
|
| 查看次数: |
233 次 |
| 最近记录: |