如何将我的子片段膨胀到 activity?

How can I inflate my child fragments into activity?

我想用视图分页器实现 SlidingTabs。我遵循了指定的步骤 in this link。我的情况只有一处不同。我正在使用片段而不是 activity。所以在这种情况下,我想在用户按下选项卡时膨胀子片段。

这是我尝试这样做的方法:

//I am using navigation drawer in my activity and when user press one item it create a fragment which I want to show many child fragments using tabs
@Override
public void onItemClick(int position) {

    switch (position){
        case 3 :
            mFragmentManager = getSupportFragmentManager();
            mFragment = new MainFragment().newInstance(mHelpLiveo.get(position).getName());

            if (mFragment != null){
                mFragmentManager.beginTransaction().replace(R.id.container, mFragment).commit();
            }
            break;
    }
}

我的 MainFragment.java 中的 onCreateView 函数:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.group_tabs, container, false);

    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.

    //EDİT 3 :
    adapter =  new ViewPagerAdapter(this.getChildFragmentManager(),Titles,Numboftabs);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) rootView.findViewById(R.id.private_public_pager);
    pager.setAdapter(adapter);

    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) rootView.findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

    // Setting Custom Color for the Scroll bar indicator of the Tab View
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.blue_grey_900);
        }
    });

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);
    return rootView;
}

在我的 ViewPagerAdapter class 中:

//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {

    if(position == 0) // if the position is 0 we are returning the First tab
    {
        Tab1 tab1 = new Tab1();
        return tab1;
    }
    else             // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
    {
        Tab2 tab2 = new Tab2();
        return tab2;
    }

}

其他与the link I provided above相同。

编辑 1:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.melomg.example.ui.MainFragment">

<com.melomg.example.ui.SlidingTabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="2dp"
    android:background="@color/myColor"/>

<android.support.v4.view.ViewPager
    android:id="@+id/private_public_pager"

    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_weight="1"
    ></android.support.v4.view.ViewPager>
</LinearLayout>

在 mainfragment.xml 文件中只有 SlidingTabLayout 出现,我看不到 Vİew Pager。

编辑 2:

一些屏幕来解释我的申请。

我的主要 activity 带有导航抽屉。当用户按下一个项目时,我的主要片段被创建

我的主要片段显示标签。但是看不到寻呼机。

在您的 mainfragment.xml 布局中,LinearLayout 方向设置为水平,因此项目在同一水平线上。 SlidingTabLayout 的宽度为 match_parent,因此 ViewPager 被挤压到宽度为 0 的右侧。只需将方向更改为 'vertical'

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.melomg.example.ui.MainFragment">

...

</LinearLayout>