How to implement Navigation Drawer and SlidingTabLayout together?

人走茶凉 提交于 2019-12-01 13:03:01

Figured it out. Followed this tutorial :

http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html

I implemented this tutorial on a fragment instead of an activity. The fragment was the first section or fragment of my Navigation Drawer. The only differences were that I wrote the code in my Fragment's layout and Java file instead of my NavDrawer Activity's files. The the Java code needs to be written in onCreateView() instead of onCreate() because its a Fragment. Secondly in the Java code of our Fragment, we need to pass the parameter getChildFragmentManager instead of getSupportFragmentManager in the constructor call of ViewPagerAdapter.

Here's the Java Code :

    package com.example.jyot.advanceparking;

    public class Menu1_Fragment extends Fragment {

ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Home","Events"};
int Numboftabs =2;

View rootView;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.menu1_layout, container, false);

    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
    adapter =  new ViewPagerAdapter(getChildFragmentManager(), Titles, Numboftabs);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) rootView.findViewById(R.id.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 the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);

    return rootView;
}

}

After this just follow the tutorial as it is.

It is a long answer but the way to do it is that use a RelativeLayout instead of FrameLayout and use the FrameLayout in the RelativeLayout.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<FrameLayout android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<!-- Sliding Tabs Go here -->

</RelativeLayout>

Here is the nice tutorial to follow with Material Design support

How To Make Material Design Sliding Tabs

Don't worry about ToolBar (if you are not using one) and do changes as per your need!

Hope that will help you and let me know if you get any error! Cheers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!