Search code examples
androidandroid-fragmentsfragment-tab-host

Fragment tabhost is not working in Fragment


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>

Solution

  • 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;
    
        }