Search code examples
androidandroid-fragmentsfragment-tab-host

FragmentTabHost and ChildFragment - ChildFragment is not showing


I am following a tutorial to make a tab navigation of two child fragments inside a fragment. But in my case, only two tabindicator is shown but no child fragment is attaching.

Parent Fragment

public class WifisFragment extends SherlockFragment {

    private static final String TAG = WifisFragment.class.getName();
    private static final String TAG1 = TAG + 1;
    private static final String TAG2 = TAG + 2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.wifis_tab_host_layout,
                container, false);

        FragmentTabHost tabHost = (FragmentTabHost) root
                .findViewById(android.R.id.tabhost);

        tabHost.setup(getSherlockActivity(), getChildFragmentManager(),
                android.R.id.tabcontent);

        Bundle arg = new Bundle();
        arg.putInt(ChildFragment.POSITION_KEY, 1);
        TabSpec tabSpec = tabHost.newTabSpec(TAG1).setIndicator("First");
        tabHost.addTab(tabSpec, ChildFragment.class, arg);

        arg = new Bundle();
        arg.putInt(ChildFragment.POSITION_KEY, 2);
        tabSpec = tabHost.newTabSpec(TAG2).setIndicator("Second");
        tabHost.addTab(tabSpec, ChildFragment.class, arg);

        return root;
    }
}

ChildFragment:

public class ChildFragment extends SherlockFragment implements OnClickListener {

    public static final String POSITION_KEY = "FragmentPositionKey";
    private int position;

    public static ChildFragment newInstance(Bundle args) {
        ChildFragment fragment = new ChildFragment();
        fragment.setArguments(args);
        return fragment;
    }

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

        position = getArguments().getInt(POSITION_KEY);
        if(CommonUtils.isDebuggable) {
            Log.d("position", "" + position);
        }
        View root = inflater.inflate(R.layout.fragment_child, container, false);
        TextView textview = (TextView) root.findViewById(R.id.textViewPosition);
        textview.setText(Integer.toString(position));
        textview.setOnClickListener(this);

        return root;
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(v.getContext(), "Clicked Position: " + position, Toast.LENGTH_LONG).show();
    }
}

XML Layout of parent fragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.neopixl.pixlui.components.textview.TextView
        android:id="@+id/header_text"
        android:background="@color/menu_color"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:minHeight="@dimen/textview_height"
        android:padding="10dp"
        android:text="@string/wifi"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        pixlui:copyandpaste="false"
        pixlui:typeface="aleo_bold.ttf" />

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal" />

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

Can you tell me what's the problem actually?


Solution

  • Nothing was problem in my code. I was using a old android support-v4 library. Then I changed the library version to latest (19.0.1) and everything is working!