Skip to content Skip to sidebar Skip to footer

How Do I Implement Appcompatactivity With Navigationdrawer And Multiple Fragments Reusing Same Tablayout?

I have successfully used TabLayout with AppCompatActivity with a CoordinatorLayout that looks close to this snippet:

Solution 1:

1. Switching between first-level Fragments

Suppose layout content.xml stands for:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".ui.MyActivity"
    tools:showIn="@layout/my_activity" />

Then, to be able to switch between the Fragments, implement this function:

privatevoidmakeTransition(int fragmentId) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        switch (fragmentId) {
            // Fragment with tabscase FRAGMENT_TABS:
                fragmentTransaction.replace(R.id.fragment_container, new TabsFragment());
                // This shows TabLayout
                findViewById(R.id.tabs).setVisibility(View.VISIBLE);
                getSupportActionBar().setTitle(R.string.fragment_tabs_title);
                break;
            // Fragment with no tabscase FRAGMENT_NO_TABS:
                fragmentTransaction.replace(R.id.fragment_container, new NoTabsFragment());
                // This hides TabLayout
                findViewById(R.id.tabs).setVisibility(View.GONE);
                getSupportActionBar().setTitle(R.string.fragment_no_tabs_title);
                break;
            default:
                thrownew RuntimeException("No fragment with ID " + fragmentId + " found");
        }
        fragmentTransaction.commit();
    }

2. Accessing and setting up TabLayout from first-level Fragment

In TabsFragment class, add a private class TabAdapter:

privateclassTabAdapterextendsFragmentPagerAdapter {

    publicTabAdapter(FragmentManager fm) {
        super(fm);
    }

    @Overridepublic CharSequence getPageTitle(int position) {
        switch (position) {
            case0:
                return"TAB1";
            case1:
                return"TAB2";
            // ...
        }
        returnnull;
    }

    @Overridepublic Fragment getItem(int position) {
        switch (position) {
            case0:
                return Tab1Fragment.getInstance();
            case1:
                return Tab2Fragment.getInstance();
            // ...
        }
        returnnull;
    }

    @OverridepublicintgetCount() {
        return2;
    }
}

Also, optionally, implement a ViewPager.OnPageChangeListener:

privateclassFragmentPageChangeListenerimplementsViewPager.OnPageChangeListener {

    @OverridepublicvoidonPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        Log.d(getClass().getName(), "onPageScrolled");
    }

    @OverridepublicvoidonPageSelected(int position) {
        Log.d(getClass().getName(), "onPageSelected");
    }

    @OverridepublicvoidonPageScrollStateChanged(int state) {
        Log.d(getClass().getName(), "onPageScrollStateChanged");
    }
}

Suppose your layout for fragment with tabs is like this:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><android.support.v4.view.ViewPagerandroid:id="@+id/viewpager"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_alignParentTop="true" /></RelativeLayout>

Override onCreateView to look like this:

@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Viewview= inflater.inflate(R.layout.viewpager_fragment, null);

    fragmentPagerAdapter = newTabAdapter(getChildFragmentManager());
    fragmentPageChangeListener = newFragmentPageChangeListener();

    ViewPagerpager= (ViewPager) view.findViewById(R.id.viewpager);
    pager.setAdapter(fragmentPagerAdapter);
    pager.addOnPageChangeListener(fragmentPageChangeListener);

    TabLayouttabLayout= (TabLayout) MyAcvtivity.getInstance().findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(pager);

    return view;
}

NB:

Use getChildFragmentManager() and not getFragmentManager() in first-level Fragments when instantiating a ViewPager.

Post a Comment for "How Do I Implement Appcompatactivity With Navigationdrawer And Multiple Fragments Reusing Same Tablayout?"