Please, explain to me... I have Navigation Drawer in my Activity and it syncs with Toolbar(like ActionBar). Activity has few fragments and in different fragments I need to use different AppBar modes (parallax in one, simple in another). So, I think that I should set CoordinatorLayout in each frament with AppBar and content. 
But how I can replace last toolbar on new to save synchronization with Drawer? Or it's wrong way and I need make it some else?

shareimprove this question

If you are using DrawerLayout and NavigationView for your navigation drawer, best solution according to me would be to use individual DrawerLayout for each of the fragment layouts and use the AppBarLayout in the body of the DrawerLayout differently for each of the fragments.

shareimprove this answer
   
Is this the only solution? I will need to re-attach almost all view hierarchy – Шах Jan 26 '16 at 13:54
   
You must understand that you are attempting to change a body view and not the parent view because the AppBarLayout remains embedded inside the body the DrawerLayout. So the best practice would be to redeclare the layout. If your NavigationView is identical for all fragments, you can write it in a separate xml layout file and use <include layout="@layout/navigation_layout_name"> which can shorten your code. – TeChNo_DeViL Jan 26 '16 at 14:01
   
Well, thank you! Just I think that this solution can take much time on fragment switching. But I will use it. – Шах Jan 26 '16 at 14:48

Not sure if my approach is good, but I tried to add this public method to my activity:

public void setToolbar(Toolbar toolbar) {
    if(toolbar != null) {
        setSupportActionBar(toolbar);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
    } else {
        drawer.setDrawerListener(null);
    }
}

and I added this in all fragments:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ((MainActivity)getActivity()).setToolbar(toolbar);
}

@Override
public void onDestroyView() {
    ((MainActivity)getActivity()).setToolbar(null);
    super.onDestroyView();
}

It's working fine, but I'm not sure if it may cause a memory leak or any other performance issue. Maybe someone can help with it?

shareimprove this answer
   
I solved this problem. Just I use DrawerLayout in activity layout and Toolbar in fragment layouts where in onCreateView set Toolbar like ActionBar and synchronize it with DrawerLayout. – ШахFeb 28 '16 at 0:44
   
And your solution has not any memory leaks) – Шах Feb 28 '16 at 0:45
1 
@Шах Would you like to elaborate on how you are syncing with DrawerLayout from fragment onCreateView? Are you re-creating the ActionBarDrawerToggle for every fragment Toolbar? Are you using the DrawerLayout constructor with, or without, Toolbar? – jayeffkay Oct 12 '16 at 11:51
   
@Шах, yes, pls, answer, how have you gotten an application with different toolbars? А то у меня выглядят ужасно. – CoolMind Dec 2 '16 at 11:37
   
@CoolMind, Просто во фрагменте, где ты определил свой Toolbar в onCreateView установи его как ActionBar и синхронизируй с DrawerLayout так же, как в обычной активити. For English, I don't remeber already how to need to define Toolbar in onCreateView to it can be used like ActionBar with DrawerLayout. – Шах Dec 2 '16 at 11:58 

You can access main DrawerLayout from each Fragment just like the following code:

    AppCompatActivity actionBar = (AppCompatActivity) getActivity();
    actionBar.setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) actionBar.findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            getActivity(), drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
shareimprove this answer
   
Can you describe an activity and fragments? Are all fragments have their own toolbars or use activity's? I have an activity with DrawerLayout and NavigationView, inside it fragments are placed, they contain CoordinatorLayout, AppBarLayout, Toolbar, but it works bad, toolbars don't look as I want when change fragments. – CoolMind Dec 2 '16 at 11:33

If you want to use appbar(toolbar) that you specified in navigation drawer in different fragments with, for example different options menu items, you could make a public method in Main Activity where you specified your navigation drawer logic.

public void setToolbar(Toolbar toolbar, String title){
    AppCompatActivity actionBar = this;
    actionBar.setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout)actionBar.findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toogle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close);
    drawer.addDrawerListener(toogle);
    toogle.setDrawerIndicatorEnabled(true);
    toogle.syncState();
    if(toolbar != null)
        toolbar.setTitle(title);
}

Now you can use this method in your fragments to access the main toolbar and override it with your custom title, options menu... You can do this by creating a new toolbar variable in your fragment and then inflate it in onCreateView method like this

toolbarFragment = (Toolbar)getActivity().findViewById(R.id.toolbar);

R.id.toolbar is the id of a toolbar that you specified in your layout file and it is the same id that you used in your main activity for the main toolbar. Now you can call the method setToolbar(Toolbar toolbar, String title) in fragment like this

((MainActivity)getActivity()).setToolbar(toolbarFragment, "Some title");

If you want to use options menu for this fragment you have to call

setHasOptionsMenu(true);

in fragments onCreate() or onCreateView() methods. After that you can override method onCreateOptionsMenu() like this

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.custom_menu1, menu);
}

You can repeat this procedure for other fragments in your navigation drawer as well. Although it worked for me, I don't know if this violates activities or fragments lifecycle or causes memory leeks.

shareimprove this answer
   
No-no, guy, it wasn't a problem. I needed to make a very flex and complex GUI for different fragments. It required full access to appbar – Шах Dec 15 '16 at 18:15
   
Just I had fragments with parallax header, navigation path (like in some file manger) and etc. – Шах Dec 15 '16 at 18:16