With the new design library there are several new layouts that change a lot how the toolbar can behave if the developer so wishes. Since different fragments have different behaviors and objectives, for example a gallery fragment with a collapsing toolbar showing an important photo, or a fragment without a scrollview that just doesn't need the appbarlayout for hiding the toolbar, having a single toolbar in the activity can prove difficult.
So with this, should I move the toolbar to each fragment? If so, I have to set the supportActionBar each time I show a fragment and also have a reference of the activity in the fragment which nullifies the independent nature of fragments. If I leave the toolbar in the Activity alone, I have to have multiple layouts defined for each type of behavior in each fragment. What would be the best approach?
As for me it sounds too weird to have appbar and toolbar in each fragment. So I've chosen to have single appbar with toolbar in activity.
To solve that issue with CoordinatorLayout you will have to set different behaviour of your
FrameLayout
(or any other Layout) that supposed to hold fragments from each fragment that you want to override default behaviour.Lets assume, that your default behaviour is
app:layout_behavior="@string/appbar_scrolling_view_behavior"
Then in your fragment_activity_layout.xml you may have something like that:
And in each fragment you wish not to implement
app:layout_behavior="@string/appbar_scrolling_view_behavior"
you will have to overrideonAttach
andonDetach
methods that will change behaviour of yourFrameLayout
:After that CoordinatorLayout won't collapse appbar, etc. and will allow fragment layouts to be full-height.