Change toolbar in fragments with Navigation Drawer template

I use NavigationDrawer template and I can switch my fragments. But I want change Toolbar functions in everything fragment. How can I set the unique toolbar actions in my fragments?

Upvotes: 0

Views: 1351

Answers (2)

Turkhan Badalov
Turkhan Badalov

Reputation: 934

I think you can setHasOptionsMenu(true) in onCreate() method of each fragment. And ovveride methods onCreateOptionsMenu() as usually. I mean inflate menu and other stuff as you always do when create menu.

Now the catch is that you must set DrawerListener to your DrawerLayout. And then into your listener ovveride methods onDrawerOpened(), and onDrawerClosed(), where in each of them you must call invalidateOptionsMenu().

Use android.support.v7.app.ActionBarDrawerToggle as your DrawerListener. Pass into method like that

myDrawerLayout.addDrawerListener(new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {

            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view)

                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);

                invalidateOptionsMenu();
            }
        };

Here R.string.drawer_close and drawer_open are just strings in resources equal to "Close" and "Open" respectively. More about navigation drawer your can read here: Navigation Drawers.

And note, you can't use unique toolbar in each fragment. I mean instance of toolbar. At least it never worked for me. Instead create some toolbar once, and then change its content on each fragment. To have access to toolbar from all fragments you can use one helper fragment called, for example, RetainFragment, that will use setRetainInstance(true). It helps you to not destroy this fragment and save into it different variables or objects. More detailed you can read in my article: Simple trick to use and manage Toolbar with Fragments in Android

Upvotes: 2

amitairos
amitairos

Reputation: 2967

Assign your NavigationView a NavigationItemSelected ClickListener and in it do something like this:

 switch (e.menuItem.itemId)
            {
                case (R.id.nav_home):
                    e.MenuItem.SetChecked(true);
                    toolbar.inflateMenu(R.menu.newMenu);
                    break;
                case (R.id.nav_two):
                    e.MenuItem.SetChecked(true);
                    toolbar.inflateMenu(R.menu.newMenu2);
                    break;
            }

And create new xml files in your menu folder that each contain different controls for your toolbar. Something like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">
 <item android:id="@+id/action_close"
        android:title=""
        android:icon="@drawable/ic_close_white_24px"
        app:showAsAction="always|collapseActionView"/>
  <item android:id="@+id/..."
        android:title=""
        android:icon="..."
        app:showAsAction="always|collapseActionView"/>
</menu>

Upvotes: 3

Related Questions