Location of coordinator with toolbar in fragments or actions

The new design library has several new layouts that greatly change the behavior of the toolbar, if the developer so desires. Since different fragments have different types of behavior and goals, for example, a fragment of a gallery with a crumbling toolbar showing an important photo, or a fragment without scrolling that just does not need an appbarlayout to hide the toolbar, having one panel in action can be difficult.

So, do I need to move the toolbar to each fragment? If so, I have to set supportActionBar every time I show the fragment, and also refer to the activity in the fragment, which negates the independent nature of the fragments. If I stay on the toolbar only in Activity, I need to have several layouts defined for each type of behavior in each fragment. What would be a better approach?

+75
android android-activity android-actionbar android-fragments
Jun 09 '15 at 18:18
source share
3 answers

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.

+39
Aug 16 '15 at 13:48
source share

Here is my solution

 <!-- Put your fragment inside a Framelayout and set the behavior for this FrameLayout --> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!-- Your fragment --> <include layout="@layout/content_main" /> </FrameLayout> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> 

+5
Oct 08 '15 at 17:09
source share

This is a very good question: should the Toolbar act like an ActionBar in an Activity or Fragment ? Having analyzed various issues and documentation, I could not find a solution that covers all cases. Therefore, it really depends on your situation in which you can go.

Case 1: The toolbar should be a replacement for ActionBar

If the toolbar should behave like a regular ActionBar (or time max. 1 fragment), I think the best / easiest way is to use the traditional Activities with your own toolbar and put your fragment in there. This way, you don’t have to worry about when the toolbar should be displayed.

Changing an ActionBar (-behaviour) from fragments is also possible, but I would not recommend it, since it makes you keep track of which fragment changed the ActionBar when. I don’t even know if an ActionBar can be done multiple times.

Case 2: each fragment should have its own (part) toolbar

You can also put different individual toolbars in different fragments with their own actions. Thus, you can display different fragments next to each other - each with its own actions on the toolbar - and suppose that it is 1 toolbar (possibly like a Gmail application, although I'm not sure). This, however, means that you will have to inflate these Toolbars yourself, but this should not be very difficult.

Hope this helps to make a choice.

(Sorry if I made any (language) errors)

+2
Jun 15 '15 at 19:01
source share



All Articles