我有一个片段,我在其中设置tabhost小部件,其中三个选项卡各有片段。 当我第一次来到这个片段时,第一个选项卡没有加载第一个子片段,但是在点击另一个选项卡并返回第一个选项卡后,它正在加载第一个子片段。 另外,我对tabhost.setup()方法中的第三个参数有什么困惑。它应该是处理制表符或第一个子片段布局的父片段的布局。
这是我的父片段代码
public class MyStoreFragment extends Fragment implements TabHost.OnTabChangeListener {
private FragmentTabHost mTabHost;
View rootView;
public MyStoreFragment() {
// Empty constructor required for fragment subclasses
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// rootView = inflater.inflate(R.layout.fragment_my_store,container, false);
// mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost = new FragmentTabHost(getActivity());
// mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
mTabHost.setup(getActivity(), getChildFragmentManager(),R.layout.fragment_my_store);
mTabHost.addTab(mTabHost.newTabSpec("new").setIndicator("NEW"), NewBookFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("trending").setIndicator("TRENDING"),FragmentTab.class, null);
mTabHost.addTab(mTabHost.newTabSpec("explore").setIndicator("EXPLORE"),FragmentTab.class, null);
mTabHost.setCurrentTab(0);
mTabHost.setOnTabChangedListener(this);
mTabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.WHITE);
mTabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#FCF4E7"));
mTabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.parseColor("#FCF4E7"));
mTabHost.getTabWidget().getChildAt(0)
.setBackgroundColor(Color.parseColor("#DF4426"));
TextView tv1 = (TextView) mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
tv1.setTextColor(Color.parseColor("#F6CFC2"));
TextView tv2 = (TextView) mTabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.title);
tv2.setTextColor(Color.parseColor("#E03C20"));
TextView tv3 = (TextView) mTabHost.getTabWidget().getChildAt(2).findViewById(android.R.id.title);
tv3.setTextColor(Color.parseColor("#E03C20"));
// TODO To set height and width properly of tab widget
ViewGroup.LayoutParams params = mTabHost.getTabWidget().getChildAt(0).getLayoutParams();
// params.width = 128;
params.height = 50;
mTabHost.getTabWidget().getChildAt(0).setLayoutParams(params );
mTabHost.getTabWidget().getChildAt(1).setLayoutParams(params );
mTabHost.getTabWidget().getChildAt(2).setLayoutParams(params );
// for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
// TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
// tv.setTextColor(Color.parseColor("#DA4426"));
// }
return mTabHost;
}