FragmentTabHost和Child Fragment - Child Fragment未显示

时间:2014-06-23 18:23:38

标签: android android-fragments fragment-tab-host

我正在按照教程在片段中制作两个子片段的标签导航。但在我的情况下,只显示了两个tabindicator,但没有附加子片段。

父片段

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布局:

<?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>

你能告诉我实际上是什么问题吗?

1 个答案:

答案 0 :(得分:0)

我的代码中没有任何问题。我使用的是旧的android support-v4库。然后我将库版本更改为最新版本(19.0.1),一切正常!