【问题标题】:How can I inflate my child fragments into activity?我怎样才能将我的孩子碎片膨胀成活动?
【发布时间】:2015-12-02 09:47:01
【问题描述】:

我想用视图寻呼机实现 SlidingTabs。我按照in this link 指定的步骤进行操作。我的情况只有一个不同。我正在使用片段而不是活动。所以在这种情况下,我想在用户按下标签时膨胀子片段。

我是如何尝试这样做的:

//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 类中:

//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:

一些屏幕来解释我的应用程序。

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

我的主要片段显示标签。但是看不到view pager。

【问题讨论】:

  • 使用 getChildFragmentManager() 代替 getFragmentManager()
  • 我在我的主要片段中使用了 this.getActivity().getSupportFragmentManager()。如何调用 getChildFragmentManager ?
  • adapter = new ViewPagerAdapter(this.getChildFragmentManager(),Titles,Numboftabs);我确实喜欢这个。但我仍然看不到我的标签片段。
  • 使用TabLayoutViewPager,它将帮助您改善项目流程。甚至你可以使用多个嵌套的 Fragment。

标签: android android-fragments android-fragmentactivity pagerslidingtabstrip


【解决方案1】:

在您的 mainfragment.xml 布局中,LinearLayout 方向设置为水平,因此项目水平位于同一行。 SlidingTabLayout 的宽度为 match_parent,因此 ViewPager 被挤压到宽度为 0 的右侧。只需将方向更改为“垂直”

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多