Android tabhost内部片段未首次加载

Android tabhost内部片段未首次加载,android,Android,我有一个片段,我在其中设置tabhost小部件,其中有三个选项卡,每个选项卡都有片段。 当我第一次来到这个片段第一个选项卡时,没有加载第一个子片段,但在单击另一个选项卡并返回第一个选项卡之后,它正在加载第一个子片段。 另外,我对tabhost.setup()方法中的第三个参数有点困惑,它应该是处理选项卡的父片段的布局,或者是第一个子片段的布局 这是我的父片段代码 public class MyStoreFragment extends Fragment implements TabHost.On

我有一个片段,我在其中设置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;

    }
公共类MyStoreFragment扩展片段实现了TabHost.OnTabChangeListener{
私有碎片选项卡主机mTabHost;
视图根视图;
公共MyStoreFragment(){
//片段子类需要空构造函数
}
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//rootView=充气机。充气(R.layout.fragment\u my\u store,container,false);
//mTabHost=(FragmentTabHost)rootView.findviewbyd(android.R.id.tabhost);
mTabHost=新的FragmentTabHost(getActivity());
//mTabHost.setup(getActivity(),getChildFragmentManager(),android.R.id.tabcontent);
mTabHost.setup(getActivity()、getChildFragmentManager()、R.layout.fragment\u my\u store);
mTabHost.addTab(mTabHost.newTabSpec(“新”).setIndicator(“新”),NewBookFragment.class,null);
mTabHost.addTab(mTabHost.newTabSpec(“趋势”).setIndicator(“趋势”),FragmentTab.class,null);
mTabHost.addTab(mTabHost.newTabSpec(“explore”).setIndicator(“explore”),FragmentTab.class,null);
mTabHost.setCurrentTab(0);
mTabHost.setOnTabChangedListener(此);
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正确设置选项卡小部件的高度和宽度
ViewGroup.LayoutParams params=mTabHost.getTabWidget().getChildAt(0.getLayoutParams();
//参数宽度=128;
参数高度=50;
mTabHost.getTabWidget().getChildAt(0).setLayoutParams(params);
mTabHost.getTabWidget().getChildAt(1).setLayoutParams(params);
mTabHost.getTabWidget().getChildAt(2.setLayoutParams(params);
//对于(int i=0;i
使用带选项卡的片段寻呼机,而不是使用此,以使用内部片段。但布局要求是三个选项卡,以及导航抽屉中的父片段如下