Implementing TabLayout with fragments in another fragment

I am having the problem, as mentioned in the title. When I implement the tabs they don't display the content, and the tab is not smooth, exactly like in this problem:

TabLayout with viewpager not smooth scrolling

So they can stop in between and so on.

What do you think am I doing wrong? I attach the code of the fragment displaying it:

 public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container, false);
        ButterKnife.bind(this,view);
        tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.news_feed)));
        tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.bio)));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        mPager.setAdapter(new PagerAdapter(getActivity().getSupportFragmentManager()));
        tabLayout.setupWithViewPager(mPager);
        return view;
    }

And the layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    tools:context="com.my.app.profileFragment">

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_below="@id/main_profile_frame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/primary"
            android:elevation="6dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
        <!--android:minHeight="?attr/actionBarSize"-->
        <!--android:layout_below="@+id/toolbar"-->

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true" />
    </RelativeLayout>



</FrameLayout>

What is wrong here? I really can't spot an error.

Thank you in advance!

Yours,

Grzegorz

Upvotes: 2

Views: 803

Answers (1)

Aswin P Ashok
Aswin P Ashok

Reputation: 711

If you want to have a fragment which behaves as a container of fragments, you must use the getChildFragmentManager() method of the fragment. If you use the getSupportFragmentManager() you will basically use the fragment manager which behaves the way the activity lifecycle goes, not the way your fragment does. (Reference)

So instead of using getActivity().getSupportFragmentManager(), use getChildFragmentManager()

Upvotes: 4

Related Questions