Fragment tabhost is not working in Fragment

你说的曾经没有我的故事 提交于 2019-12-13 16:40:42

问题


I am following this turtorial http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

here in Homefragment i set the fragmenttabhost,the issue is the view of tab is not displaying

public class HomeFragment extends Fragment {


    private FragmentTabHost mTabHost;

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.my_parent_fragment, container, false);

        mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getFragmentManager(), android.R.id.tabcontent);

        mTabHost.addTab(
                mTabHost.newTabSpec("tab1").setIndicator("DISCOVER", null),
                FragmentA.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab2").setIndicator("SHOP", null),
                FragmentB.class, null);

        return rootView;
    }


}

FragmentA.java

public class FragmentA extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fraga, container, false);
        Toast.makeText(getActivity(), "DISCOVER", Toast.LENGTH_LONG).show();
        return rootView;
    }

}

fraga.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    >

    <TextView
        android:id="@+id/txtLabel"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="16dp"
        android:text="DISCOVER"/>



</RelativeLayout>

回答1:


Try this..

public class HomeFragment extends Fragment {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    private FragmentTabHost tabHost;

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        tabHost = new FragmentTabHost(getActivity());
        tabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment);

        Bundle arg1 = new Bundle();
        arg1.putInt("Arg for Frag1", 1);
        tabHost.addTab(tabHost.newTabSpec("Tab1").setIndicator("Tab1")),
                FragmentA.class, arg1);

        Bundle arg2 = new Bundle();
        arg2.putInt("Arg for Frag2", 2);
        tabHost.addTab(tabHost.newTabSpec("Tab2").setIndicator("Tab 2")),
            FragmentB.class, arg2);


        return tabHost;

    }


来源:https://stackoverflow.com/questions/31782797/fragment-tabhost-is-not-working-in-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!