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:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/dashboard_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.Toolbar"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/dashboard_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
And in each fragment you wish not to implement app:layout_behavior="@string/appbar_scrolling_view_behavior"
you will have to override onAttach
and onDetach
methods that will change behaviour of your FrameLayout
:
CoordinatorLayout.Behavior behavior;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if(behavior != null)
return;
FrameLayout layout =(FrameLayout) getActivity().findViewById(R.id.dashboard_content);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) layout.getLayoutParams();
behavior = params.getBehavior();
params.setBehavior(null);
}
@Override
public void onDetach() {
super.onDetach();
if(behavior == null)
return;
FrameLayout layout =(FrameLayout) getActivity().findViewById(R.id.dashboard_content);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) layout.getLayoutParams();
params.setBehavior(behavior);
layout.setLayoutParams(params);
behavior = null;
}
After that CoordinatorLayout won't collapse appbar, etc. and will allow fragment layouts to be full-height.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…