How to implement Navigation Drawer and SlidingTabLayout together?

后端 未结 2 491
终归单人心
终归单人心 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.

提交回复
热议问题