title: AppBarLayout hiding toolbar on scroll behavior, switching fragments
body: <p>I've implemented a <code>CoordinatorLayout</code> and wrapped my <code>Toolbar</code> inside an <code>AppBarLayout</code> so that the toolbar hides itself when scrolling. Most of the app consists of Fragments that are swapped in and out of the <code>@id/container</code> FrameLayout as seen below. Some of the Fragments are just <code>RecyclerViews</code>, while others are other layouts. I've added <code>app:layout_behavior="@string/appbar_scrolling_view_behavior"</code> to the <code>@id/container</code> FrameLayout. Heres my main layout:</p>
  
  <pre><code>&lt;DrawerLayout&gt;
  
       &lt;RelativeLayout
          android:id="@+id/mainRelativeLayout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          &gt;
  
          &lt;android.support.design.widget.CoordinatorLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"  
              &gt;
  
              &lt;android.support.design.widget.AppBarLayout
                  android:id="@+id/appBarLayout"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  &gt;
  
                  &lt;Toolbar
                      android:id="@+id/toolbar"
                      android:layout_width="match_parent"
                      android:layout_height="?attr/actionBarSize"
                      android:elevation="5dp"
                      app:layout_scrollFlags="scroll|enterAlways"
                      &gt;
  
                  &lt;/Toolbar&gt;
  
              &lt;/android.support.design.widget.AppBarLayout&gt;
  
              &lt;FrameLayout
                  android:id="@+id/container"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  app:layout_behavior="@string/appbar_scrolling_view_behavior"
                  /&gt;
  
          &lt;/android.support.design.widget.CoordinatorLayout&gt;
  
      &lt;/RelativeLayout&gt;
  
  &lt;!-- ignore --&gt;
  &lt;drawercontents&gt;
  &lt;/DrawerLayout&gt;
  </code></pre>
  
  <p>Now, the <code>Toolbar</code> hides itself when scrolling on Fragments with a <code>RecyclerView</code>.</p>
  
  <p>The issue is, when switching to another fragment that doesnt have a <code>RecyclerView</code>, it will mess up that fragments layout. For example, if I scroll down on a fragment that containers a <code>RecyclerView</code>, the <code>Toolbar</code> will hide itself (like it should). Then, if I switch to the other fragment without the <code>RecyclerView</code>, the toolbar will still be hidden, and the layout will expand into that extra space. If I start back at a fragment with a <code>RecyclerView</code> and dont scroll (so the <code>Toolbar</code> stays visible), and then switch to another fragment, the content is now pushed down off the screen because of the <code>Toolbar</code>. </p>
  
  <p>Heres a screenshot of the issue: <a href="http://prnt.sc/a7majo" rel="nofollow">http://prnt.sc/a7majo</a></p>
  
  <p>I also tried disabling the scroll behavior in that fragment using the following code:</p>
  
  <pre><code>Toolbar mToolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
  
  AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) mToolbar.getLayoutParams();
  toolbarLayoutParams.setScrollFlags(0);
  mToolbar.setLayoutParams(toolbarLayoutParams);
  
  AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appBarLayout);
  
  CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
  appBarLayoutParams.setBehavior(null);
  appBarLayout.setLayoutParams(appBarLayoutParams);
  </code></pre>
  
  <p>This doesnt work either an produces the odd result pictured at the bottom here: <a href="http://prnt.sc/a7mapg" rel="nofollow">http://prnt.sc/a7mapg</a> </p>
  
  <p>So whats the workaround for this? I've seen a few other questions that were similar to this but the answers given did not seem to work for my situation. Thanks in advance!</p>