FragmentTabHost未在Android中的Fragment内部创建视图 [英] FragmentTabHost not creating view inside Fragment in android
问题描述
我在使视图在Tabhost上更改时遇到问题-当我选择一个选项卡时,内容保持空白。
I am having an issue getting the view to change on a tabhost - when I select a tab the content stays blank.
据我所知, onCreateView
不在子片段上被调用。 onMenuCreate
可以正常运行,因为菜单应该按照预期进行更改。
From what I can tell, onCreateView
is not being called on the child Fragments. onMenuCreate
runs fine because the menu changes like it is supposed to.
public class PatientTabFragment extends Fragment {
private FragmentTabHost mTabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager());
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Info"),
NewPatientFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Notes"),
NoteListFragment.class, null);
return mTabHost;
}
@Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
}
推荐答案
到文档:
特殊的TabHost,允许将Fragment对象用于其Tab
的内容。将其放置在视图层次结构中时, 在扩大
层次结构后,必须将setup(Context,FragmentManager,int)调用为
以完成选项卡宿主的初始化。
Special TabHost that allows the use of Fragment objects for its tab content. When placing this in a view hierarchy, after inflating the hierarchy you must call setup(Context, FragmentManager, int) to complete the initialization of the tab host.
(强调我的意思)
所以我建议像这样:
public class PatientTabFragment extends Fragment {
private FragmentTabHost mTabHost;
private boolean createdTab = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager());
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Info"),
NewPatientFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Notes"),
NoteListFragment.class, null);
return mTabHost;
}
public void onResume(){
if (!createdTab){
createdTab = true;
mTabHost.setup(getActivity(), getActivity().
getSupportedFragmentManager());
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
}
这篇关于FragmentTabHost未在Android中的Fragment内部创建视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!