Same Navigation Drawer in different Activities

前端 未结 12 1438
执念已碎
执念已碎 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

    <?xml version="1.0" encoding="utf-8"?>
    
        <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:elevation="4dp"
            android:id="@+id/toolbar"
            android:theme="@style/ThemeOverlay.AppCompat.Dark"
    
    
            >
    
        </android.support.v7.widget.Toolbar>
    

    use this for navigation header if want to use

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="192dp"
        android:background="?attr/colorPrimaryDark"
        android:padding="16dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark"
        android:orientation="vertical"
        android:gravity="bottom">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:id="@+id/navhead"
            android:orientation="vertical"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">
    
            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:textColor="#ffffff"
                android:text="tanya"
                android:textSize="14sp"
                android:textStyle="bold"
    
                />
    
            <TextView
                android:id="@+id/email"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#ffffff"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="5dp"
                android:text="tanya.com"
                android:textSize="14sp"
                android:textStyle="normal"
    
                />
        </LinearLayout>
        <de.hdodenhof.circleimageview.CircleImageView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_below="@+id/imageView"
            android:layout_marginTop="15dp"
    
            android:src="@drawable/face"
            android:id="@+id/circleView"
            />
    
    
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-11-22 05:05

    So this answer is a few years late but someone may appreciate it. Android has given us a new widget that makes using one navigation drawer with several activities easier.

    android.support.design.widget.NavigationView is modular and has its own layout in the menu folder. The way that you use it is to wrap xml layouts the following way:

    1. Root Layout is a android.support.v4.widget.DrawerLayout that contains two children: an <include ... /> for the layout that is being wrapped (see 2) and a android.support.design.widget.NavigationView.

      <android.support.v4.widget.DrawerLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/drawer_layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true"
          tools:openDrawer="start">
      
      <include
          layout="@layout/app_bar_main"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
      
      <android.support.design.widget.NavigationView
          android:id="@+id/nav_view"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:fitsSystemWindows="true"
          app:headerLayout="@layout/nav_header_main"
          app:menu="@menu/activity_main_drawer" />
      

    nav_header_main is just a LinearLayout with orientation = vertical for the header of your Navigation Drawar.

    activity_main_drawer is a menu xml in your res/menu directory. It can contain items and groups of your choice. If you use the AndroidStudio Gallery the wizard will make a basic one for you and you can see what your options are.

    1. App bar layout is usually now a android.support.design.widget.CoordinatorLayout and this will include two children: a android.support.design.widget.AppBarLayout (which contains a android.support.v7.widget.Toolbar) and an <include ... > for your actual content (see 3).

      <android.support.design.widget.CoordinatorLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context="yourpackage.MainActivity">
      
       <android.support.design.widget.AppBarLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:theme="@style/AppTheme.AppBarOverlay">
      
          <android.support.v7.widget.Toolbar
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="?attr/colorPrimary"
              app:popupTheme="@style/AppTheme.PopupOverlay" />
      
      </android.support.design.widget.AppBarLayout>
      
      <include layout="@layout/content_main" />
      

    2. Content layout can be whatever layout you want. This is the layout that contains the main content of the activity (not including the navigation drawer or app bar).

    Now, the cool thing about all of this is that you can wrap each activity in these two layouts but have your NavigationView (see step 1) always point to activity_main_drawer (or whatever). This means that you will have the same(*) Navigation Drawer on all activities.

    • They won't be the same instance of NavigationView but, to be fair, that wasn't possible even with the BaseActivity solution outlined above.
    0 讨论(0)
  • 2020-11-22 05:05

    My suggestion is: do not use activities at all, instead use fragments, and replace them in the container (Linear Layout for example) where you show your first fragment.

    The code is available in Android Developer Tutorials, you just have to customize.

    http://developer.android.com/training/implementing-navigation/nav-drawer.html

    It is advisable that you should use more and more fragments in your application, and there should be only four basic activities local to your application, that you mention in your AndroidManifest.xml apart from the external ones (FacebookActivity for example):

    1. SplashActivity: uses no fragment, and uses FullScreen theme.

    2. LoginSignUpActivity: Do not require NavigationDrawer at all, and no back button as well, so simply use the normal toolbar, but at the least, 3 or 4 fragments will be required. Uses no-action-bar theme

    3. HomeActivity or DashBoard Activity: Uses no-action-bar theme. Here you require Navigation drawer, also all the screens that follow will be fragments or nested fragments, till the leaf view, with the shared drawer. All the settings, user profile and etc. will be here as fragments, in this activity. The fragments here will not be added to the back stack and will be opened from the drawer menu items. In the case of fragments that require back button instead of the drawer, there is a fourth kind of activity below.

    4. Activity without drawer. This activity has a back button on top and the fragments inside will be sharing the same action-bar. These fragments will be added to the back-stack, as there will be a navigation history.

    [ For further guidance see: https://stackoverflow.com/a/51100507/787399 ]

    Happy Coding !!

    0 讨论(0)
  • 2020-11-22 05:09

    Easiest way to reuse a common Navigation drawer among a group of activities

    app_base_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <FrameLayout
            android:id="@+id/view_stub"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        </FrameLayout>
    
        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/menu_test"
            />
    </android.support.v4.widget.DrawerLayout>
    

    AppBaseActivity.java

    /*
    * This is a simple and easy approach to reuse the same 
    * navigation drawer on your other activities. Just create
    * a base layout that conains a DrawerLayout, the 
    * navigation drawer and a FrameLayout to hold your
    * content view. All you have to do is to extend your 
    * activities from this class to set that navigation 
    * drawer. Happy hacking :)
    * P.S: You don't need to declare this Activity in the 
    * AndroidManifest.xml. This is just a base class.
    */
    import android.content.Intent;
    import android.content.res.Configuration;
    import android.os.Bundle;
    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.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.FrameLayout;
    
    public abstract class AppBaseActivity extends AppCompatActivity implements MenuItem.OnMenuItemClickListener {
        private FrameLayout view_stub; //This is the framelayout to keep your content view
        private NavigationView navigation_view; // The new navigation view from Android Design Library. Can inflate menu resources. Easy
        private DrawerLayout mDrawerLayout;
        private ActionBarDrawerToggle mDrawerToggle;
        private Menu drawerMenu;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            super.setContentView(R.layout.app_base_layout);// The base layout that contains your navigation drawer.
            view_stub = (FrameLayout) findViewById(R.id.view_stub);
            navigation_view = (NavigationView) findViewById(R.id.navigation_view);
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 0, 0);
            mDrawerLayout.setDrawerListener(mDrawerToggle);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
            drawerMenu = navigation_view.getMenu();
            for(int i = 0; i < drawerMenu.size(); i++) {
              drawerMenu.getItem(i).setOnMenuItemClickListener(this);
            }
            // and so on...
        }
    
        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            mDrawerToggle.syncState();
        }
    
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            mDrawerToggle.onConfigurationChanged(newConfig);
        }
    
        /* Override all setContentView methods to put the content view to the FrameLayout view_stub
         * so that, we can make other activity implementations looks like normal activity subclasses.
         */
        @Override
        public void setContentView(int layoutResID) {
            if (view_stub != null) {
                LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
                View stubView = inflater.inflate(layoutResID, view_stub, false);
                view_stub.addView(stubView, lp);
            }
        }
    
        @Override
        public void setContentView(View view) {
            if (view_stub != null) {
                ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
                view_stub.addView(view, lp);
            }
        }
    
        @Override
        public void setContentView(View view, ViewGroup.LayoutParams params) {
            if (view_stub != null) {
                view_stub.addView(view, params);
            }
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Pass the event to ActionBarDrawerToggle, if it returns
            // true, then it has handled the app icon touch event
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            // Handle your other action bar items...
    
            return super.onOptionsItemSelected(item);
        }
    
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.item1:
                    // handle it
                    break;
                case R.id.item2:
                    // do whatever
                    break;
                // and so on...
            }
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 05:15

    My answer is just a conceptual one without any source code. It might be useful for some readers like myself to understand.

    It depends on your initial approach on how you architecture your app. There are basically two approaches.

    1. You create one activity (base activity) and all the other views and screens will be fragments. That base activity contains the implementation for Drawer and Coordinator Layouts. It is actually my preferred way of doing because having small self-contained fragments will make app development easier and smoother.

    2. If you have started your app development with activities, one for each screen , then you will probably create base activity, and all other activity extends from it. The base activity will contain the code for drawer and coordinator implementation. Any activity that needs drawer implementation can extend from base activity.

    I would personally prefer avoiding to use fragments and activities mixed without any organizing. That makes the development more difficult and get you stuck eventually. If you have done it, refactor your code.

    0 讨论(0)
  • 2020-11-22 05:16

    update this code in baseactivity. and dont forget to include drawer_list_header in your activity xml.

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    setContentView(R.layout.drawer_list_header);
    

    and dont use request() in your activity. but still the drawer is not visible on clicking image..and by dragging it will visible without list items. i tried a lot but no success. need some workouts for this...

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