Android: Navigation-Drawer on all activities

前端 未结 3 1228
醉酒成梦
醉酒成梦 2020-12-01 15:12

I want to add navigation drawer on all actvities of my Android project. This is the code of the MainActivity:

public class MainActivity extends Activity {

         


        
相关标签:
3条回答
  • 2020-12-01 16:01

    Okay Guys will share my way I do it (in case you are developing project after somebody and there is already tons of activities).

    Finally I inflate navigation drawer with one line of code (in onCreate):

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mContext = this; //context
            //setContentView(R.layout.activity_favorites); // Was so
            Utils.inflatedNavigationDrawerView(mContext, R.layout.activity_favorites); // Now is
            //...
        }
    

    next what is Utils? :

    public class Utils{
    
    public static void inflatedNavigationDrawerView(Context context, int layoutId){
            // Here get view of clear DrawerLayout
            final View drawerLayout = View.inflate(context,R.layout.navigation_drawer_inflator, null);
            // Next inflate to it passed layout for activity
            final View mainLayout = View.inflate(context,layoutId, (ViewGroup) drawerLayout);
            // And finally inflate to it fragment for NavigationDraver
            final View fragmentLayout = View.inflate(context,R.layout.navigation_drawer_inflator_fragment, (ViewGroup) drawerLayout);
    
            // Next we should try to get our drawer (should check with casting to each activity you want to insert to)
            NavigationDrawerFragment drawerFragment = null;
    
            // this block should be repeated for each activity you want to use in.    
            if (context instanceof FavoritesActivity){
                // Set our pack of inflates to contentview
                ((FavoritesActivity) context).setContentView(mainLayout);
                // And now we get NavigationDrawerFragment
                drawerFragment = (NavigationDrawerFragment)
                        ((FavoritesActivity) context).getSupportFragmentManager().findFragmentById(R.id.navigation_drawer_fragment);
            }
    
            if(drawerFragment != null){ // if null then we missed some castings for activity used in.
                // Finally setup our drawer
                drawerFragment.setUpEasy(R.id.navigation_drawer_fragment, (DrawerLayout)drawerLayout.findViewById(R.id.drawer_layout), (Toolbar) mainLayout.findViewById(R.id.app_bar));
            }
        }
    }
    

    If you would like to avoid creation of hamburger in Toolbar then you shoud in setUpEasy place: mDrawerToggle.setDrawerIndicatorEnabled(false);

    Here is my sample of setup:

    public void setUpEasy(int fragmentId, DrawerLayout drawerLayout, Toolbar toolBar) {
            mContainerView = getActivity().findViewById(fragmentId);
            mDrawerLayout = drawerLayout;
    
    
            mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolBar, R.string.open, R.string.close){
    
                @Override
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
                    if(!mUserLearnedDrawer){
                        mUserLearnedDrawer = true;
                        saveToPreferences(getActivity(), Constants.PREFERENCES_LEARNDRAWER_KEY, mUserLearnedDrawer+"");
                    }
                }
    
                @Override
                public void onDrawerClosed(View drawerView) {
                    super.onDrawerClosed(drawerView);
                }
    
    
            };
            mDrawerToggle.setDrawerIndicatorEnabled(false); // If false then no hamburger menu.
            mDrawerLayout.setDrawerListener(mDrawerToggle);
            mDrawerLayout.post(new Runnable() {
                @Override
                public void run() {
                    mDrawerToggle.syncState();
                }
            });
        }
    

    and views used:

    // navigation_drawer_inflator.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:dragAndDrop="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    </android.support.v4.widget.DrawerLayout>
    

    // navigation_drawer_inflator_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/navigation_drawer_fragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_marginTop="56dp"
        app:layout="@layout/fragment_navigation_drawer"
        tools:layout="@layout/fragment_navigation_drawer" />
    

    Finally if you will implement this - You will be able to insert navigation drawer to any activity on run. Cheers :)

    0 讨论(0)
  • 2020-12-01 16:04

    Here's how I did it. Create the helper class. I also added some optional code for the ability to finish() the class.

    public class NavDrawerHelper extends ContextWrapper{
    
    public NavDrawerHelper(Context context){
        super(context);
    }
    
        public void initNav(final DrawerLayout drawerLayout, NavigationView navigationView, Toolbar toolbar, final boolean isFinish){
    
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int id = menuItem.getItemId();
                switch (id){
                    case R.id.nav_home:
                        startActivity(new Intent(getBaseContext(), MainActivity.class));
                        if (isFinish) ((Activity)getBaseContext()).finish();
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.nav_settings:
                        startActivity(new Intent(getBaseContext(), SettingsActivity.class));
                        if (isFinish) ((Activity)getBaseContext()).finish();
                        drawerLayout.closeDrawers();
                        break;
                }
                return true;
            }
        });
    
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(((Activity)getBaseContext()),drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){
            @Override
            public void onDrawerClosed(View v){
                super.onDrawerClosed(v);
            }
            @Override
            public void onDrawerOpened(View v) {
                super.onDrawerOpened(v);
            }
        };
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
    }
    

    }

    then add this code to all activities. This replaces your existing initNavigationDrawer() method.

        public void initNavigationDrawer() {
        //views
        NavigationView navigationView = findViewById(R.id.navigation_view);
        DrawerLayout drawerLayout = findViewById(R.id.drawer);
    
        NavDrawerHelper navDrawerHelper = new NavDrawerHelper(this);
        navDrawerHelper.initNav(drawerLayout, navigationView, toolbar, false);
    

    }

    0 讨论(0)
  • 2020-12-01 16:07

    The easy way is that you should create fragments. If you are ready to for little hard thing then this is for you. It will let you have same navigation drawer in all activities.

    Create drawer_n_activity.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
    <FrameLayout
        android:id="@+id/drawer_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    <YourDrawer
        android:id="@+id/drawer_drawer"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >
    
    </YourDrawer>
    
    </RelativeLayout>
    

    Your DrawerActivity.class

    public class DrawerActivity extends Activity {
    
        public RelativeLayout fullLayout;
        public FrameLayout frameLayout;
    
        @Override
        public void setContentView(int layoutResID) {
    
            fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.drawer_n_activity, null);
            frameLayout = (FrameLayout) fullLayout.findViewById(R.id.drawer_frame);
    
            getLayoutInflater().inflate(layoutResID, frameLayout, true);
    
            super.setContentView(fullLayout);
    
            //Your drawer content...
    
        }
    }
    

    Now, to include same Navigation Drawer in all your activities and mind one more thing, all your activities must extend DrawerActivity

    public class MainActivity extends DrawerActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main); //layout for 1st activity
       }
    }
    
    public class SecondActivity extends DrawerActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second_activity); //layout for 2nd activity
       }
    }
    
    0 讨论(0)
提交回复
热议问题