How to impliment navigation drawer and swipe tab in same activity in android studio?

前端 未结 2 1946
暖寄归人
暖寄归人 2020-12-09 14:06

I have done both separately and I want to combine the both activity in single activity. I can\'t get any idea what should I do. Please please help me.

MainAc

2条回答
  •  心在旅途
    2020-12-09 15:00

    You can easily integrate a NavigationView along with ViewPager for navigation menu and swype-enabled views respectively. Example implementation would be:

    Public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    
        private DrawerLayout mDrawerLayout;
        ...
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    
            // Set up the ViewPager with the sections adapter.
            mViewPager = (ViewPager) findViewById(R.id.container);
            mViewPager.setAdapter(mSectionsPagerAdapter);
    
            // Set up the tabs for ViewPager
            TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
            tabLayout.setupWithViewPager(mViewPager);
    
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();
    
            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
        }
    
        ...
        //Do other stuff like defining the viewpager, assigning navigation menu tasks, opening and closing the drawer etc etc
    

    And in your layout, make sure the layout looks something like:

    
    
    
        
    
            
    
            
    
                
    
            
    
            
    
            
    
        
    
        
    
    
    

提交回复
热议问题