How to implement Navigation Drawer and SlidingTabLayout together?

后端 未结 2 489
终归单人心
终归单人心 2021-01-15 23:33

I have implemented the Navigation Drawer. This is activity_nav_drawer :

    

        
相关标签:
2条回答
  • 2021-01-16 00:16

    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.

    0 讨论(0)
  • 2021-01-16 00:20

    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

    0 讨论(0)
提交回复
热议问题