For me, that sounds too weird when every snippet has a toolbar and a toolbar. So I decided to have a separate application bar with a toolbar in action.
To solve this problem with CoordinatorLayout, you will have to set a different behavior for your FrameLayout (or any other layout) that should contain fragments from each fragment that you want to override by default.
Suppose your default behavior is app:layout_behavior="@string/appbar_scrolling_view_behavior"
Then, in your fragment_activity_layout.xml file, there could be something like this:
<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 that you do not want to implement app:layout_behavior="@string/appbar_scrolling_view_behavior" , you will have to override the onAttach and onDetach methods that will change the behavior 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 will not hide the application bar, etc., and will allow full-text fragment layouts.
Klaus Schwartz Aug 16 '15 at 13:48 2015-08-16 13:48
source share