41

I use support design library to show/hide toolbar when scrolling a recyclerView inside a fragment, as mention here https://github.com/codepath/android_guides/wiki/Handling-Scrolls-with-CoordinatorLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/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:popupTheme="@style/AppTheme.PopupOverlay"
                app:layout_scrollFlags="scroll|enterAlways"/>

        </android.support.design.widget.AppBarLayout>


        <FrameLayout
            android:id="@+id/flContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_dialog_email" />

    </android.support.design.widget.CoordinatorLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

It works grate, Now I want to enable/disable toolbar scrolling programmatically because I use fragments and I need to stop this feature for some fragments.

Mohamad Shaker
  • 1,466
  • 2
  • 14
  • 21

2 Answers2

73

The Toolbar, being a child of the AppBarLayout, gets its LayoutParams from the AppBarLayout. These layout params have the scroll flags that are set in the XML.

So, you get the AppBarLayout.LayoutParams from the Toolbar, and call setScrollFlags() to change the flags to the value you want.

    Toolbar toolbar = findViewById(R.id.toolbar);  // or however you need to do it for your code
    AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
    params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL);  // clear all scroll flags
Mostafa Gazar
  • 2,487
  • 18
  • 23
kris larson
  • 30,387
  • 5
  • 62
  • 74
  • 4
    And to add the scrolling back at runtime, use the same method with the constants [from here](http://developer.android.com/reference/android/support/design/widget/AppBarLayout.LayoutParams.html#SCROLL_FLAG_ENTER_ALWAYS) – Mauker Dec 31 '15 at 02:13
  • I want the same behavior. It worked but the problem I'm having is that, when I set the flags to 0, the nestedScrollView's height is reduced and it creates a huge blank space at the bottom of the content. Please help if someone knows about it. – Vineet Ashtekar Feb 26 '16 at 09:30
  • @VineetAshtekar You could try taking the appbar_scrolling_view_behavior off the `NestedScrollView` at the same time. Since the toolbar can no longer scroll, there's no reason to lay out the `NestedScrollView` as if it can. I don't know exactly how to do that programmatically; I am looking into it. – kris larson Feb 27 '16 at 15:43
  • I'm using the same lines of code to change the scroll flags of my CollapsingToolbarLayout. But I need to trigger this code when AppBarLayout is touched by the user. However, my issue is that the changing scroll flags would be effective until I release the touch. Any ideas to change the scroll flags immediately while the user still touching the AppBarLayout? – OrangeTree Jun 06 '16 at 21:18
  • Never mind. I was able to fix the above issue by programmatically adding fake ACTION_CANCEL and ACTION_DOWN events after changing scroll flags. – OrangeTree Jun 06 '16 at 21:39
  • For some reason this is not working for me when I'm using a `RecyclerView`. – Mauker Feb 01 '17 at 13:59
  • 1
    After support 25.3.1, it need to invoke `appBarLayout.requestLayout()` to take effects. – Chaos May 02 '17 at 13:29
12

Remove app:layout_scrollFlags="scroll|enterAlways" wherever you want to disable taskbar scrolling.

Anuraag Baishya
  • 874
  • 2
  • 11
  • 26