Same Navigation Drawer in different Activities

前端 未结 12 1578
执念已碎
执念已碎 2020-11-22 04:24

I made a working navigation drawer like it\'s shown in the tutorial on the developer.android.com website. But now, I want to use one Navigation Drawer, i created in the Navi

12条回答
  •  余生分开走
    2020-11-22 05:04

    package xxxxxx;
    
    
    
    import android.app.SearchManager;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.SearchView;
    import android.support.design.widget.NavigationView;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Toast;
    
    
    public class loginhome extends AppCompatActivity {
        private Toolbar toolbar;
        private NavigationView navigationView;
        private DrawerLayout drawerLayout;
    
        // Make sure to be using android.support.v7.app.ActionBarDrawerToggle version.
        // The android.support.v4.app.ActionBarDrawerToggle has been deprecated.
        private ActionBarDrawerToggle drawerToggle;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.loginhome);
    
            // Initializing Toolbar and setting it as the actionbar
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
    
            //Initializing NavigationView
    
    
            navigationView = (NavigationView) findViewById(R.id.nav_view);
    
            //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
            navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    
                // This method will trigger on item Click of navigation menu
    
                public boolean onNavigationItemSelected(MenuItem menuItem) {
    
    
                    //Checking if the item is in checked state or not, if not make it in checked state
                    if(menuItem.isChecked()) menuItem.setChecked(false);
                    else menuItem.setChecked(true);
    
                    //Closing drawer on item click
                    drawerLayout.closeDrawers();
    
                    //Check to see which item was being clicked and perform appropriate action
                    switch (menuItem.getItemId()){
    
    
                        //Replacing the main content with ContentFragment Which is our Inbox View;
                        case R.id.nav_first_fragment:
                            Toast.makeText(getApplicationContext(),"First fragment",Toast.LENGTH_SHORT).show();
                             FirstFragment fragment = new FirstFragment();
                            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                            fragmentTransaction.replace(R.id.frame,fragment);
                            fragmentTransaction.commit();
                            return true;
    
                        // For rest of the options we just show a toast on click
                        case R.id.nav_second_fragment:
                            Toast.makeText(getApplicationContext(),"Second fragment",Toast.LENGTH_SHORT).show();
                            SecondFragment fragment2 = new SecondFragment();
                            android.support.v4.app.FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
                            fragmentTransaction2.replace(R.id.frame,fragment2);
                            fragmentTransaction2.commit();
                            return true;
    
                        default:
                            Toast.makeText(getApplicationContext(),"Somethings Wrong",Toast.LENGTH_SHORT).show();
                            return true;
    
                    }
                }
            });
    
            // Initializing Drawer Layout and ActionBarToggle
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open, R.string.drawer_close){
    
                @Override
                public void onDrawerClosed(View drawerView) {
                    // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
                    super.onDrawerClosed(drawerView);
                }
    
                @Override
                public void onDrawerOpened(View drawerView) {
                    // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
    
                    super.onDrawerOpened(drawerView);
                }
            };
    
            //Setting the actionbarToggle to drawer layout
            drawerLayout.setDrawerListener(actionBarDrawerToggle);
    
            //calling sync state is necessay or else your hamburger icon wont show up
            actionBarDrawerToggle.syncState();
    
    
    
    
    
    
    
        }
    

    use this for your toolbar.xml

    
    
        
    
        
    

    use this for navigation header if want to use

    
        
    
            
    
            
        
        
    
    
    
    
    

提交回复
热议问题