扩展片段的类中的选项卡

时间:2014-11-23 21:25:18

标签: android tabs fragment

我正在阅读许多教程如何在扩展FragmentActivity的类中实现制表符,但没有关于如何在扩展Fragment的类中实现制表符的人
我使用AndroidStudio创建了一个带有NavigationDrawerFragment的新项目:为其显示的每个选项菜单创建一个新的片段。此navigationDrawerFragment。在这个片段里面我需要有一些标签。

如何在片段的onCreateView方法中添加标签?
有人可以给我一个非常简单的例子吗?


我正在努力解决我的问题:

第1步 - 我创建了一个新的LinearLayout,它包含tabHost,它是主要片段:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment1"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TabHost
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabHost"
        android:layout_gravity="center_horizontal">

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

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
            </TabWidget>
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="horizontal">
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="horizontal">
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="horizontal">
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

第2步 - 在主要片段上我放了这行代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            //View rootView = inflater.inflate(R.layout.vediamo, container, false);

            mTabHost = new FragmentTabHost(getActivity());
            mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1);

            mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                    CreaAnnoFragment.class, null);
            mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
                    ModificaAnnoFragment.class, null);
            mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
                    EliminaAnnoFragment.class, null);

            return mTabHost;
        }

        @Override
        public void onDestroyView() {
            super.onDestroyView();
            mTabHost = null;
        }

如果我运行该程序,我会收到此错误:

java.lang.NullPointerException
        at myapps.studentsmarks.CreaAnnoFragment.onAttach(CreaAnnoFragment.java:100)

FIXED - 解决方案
“包装的片段”不需要onAttach()方法,这是逻辑。所以只需删除它

1 个答案:

答案 0 :(得分:1)

为此,您可以使用FragmentTabHost课程,这样就可以为Fragment添加标签,就像您对Activities所做的那样。

以下是一个例子:

public class FragmentTabsFragmentSupport extends Fragment {
private FragmentTabHost mTabHost;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    mTabHost = new FragmentTabHost(getActivity());
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1);

    mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
            FirstFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
            SecondFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
            ThirdFragment.class, null);

    return mTabHost;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    mTabHost = null;
}

}

有关详细信息,请查看official documentation

相关问题