How to set app:layout_scrollFlags for Toolbar programmatically

34,477

Solution 1

I'd strongly recommend against changing the scrolling flags based on what tab is selected - having the Toolbar automatically return (and the content move down) when scrolling to a non-recyclerview tab can be very jarring and probably not an interaction pattern you want (exasperated if your two RecyclerView tabs are next to one another).

However, if you want to see it in person, you can use setScrollFlags() to set the scroll flags programmatically:

Toolbar toolbar = ... // your toolbar within an AppBarLayout
AppBarLayout.LayoutParams params = 
    (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
    | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);

In order to clear flags

params.setScrollFlags(0)

Solution 2

// Show toolbar when we are in maps mode
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) mToolbar.getLayoutParams();
CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
if(isMapIndex) {
    params.setScrollFlags(0);
    appBarLayoutParams.setBehavior(null);
    mAppBarLayout.setLayoutParams(appBarLayoutParams);
} else {
    params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
    appBarLayoutParams.setBehavior(new AppBarLayout.Behavior());
    mAppBarLayout.setLayoutParams(appBarLayoutParams);
}
Share:
34,477
Hoang Ha
Author by

Hoang Ha

Updated on October 01, 2020

Comments

  • Hoang Ha
    Hoang Ha over 3 years

    My app have a viewpager and 4 tabs, each tab have many fragment. But I just want my Toolbar scroll up/down while scrolling recyclerview in 2 specific tabs. But I don't know how to block Toolbar scroll for other tabs. I tried to import toolbar for each fragment but it seems I can't do it. Anyone have idea to solve this problem?

  • Hoang Ha
    Hoang Ha almost 9 years
    Thank you. I don't want to make it that way but my stupid client want it :D.
  • android developer
    android developer almost 9 years
    Is it possible to let the scrolling work (meaning hide&show of elements, like the app-bar ) for ListView and other views that have scrolling?
  • marmor
    marmor almost 9 years
    It does make sense to play with scroll flags in specific scenarios like when entering search mode (Toolbar's ActionMode)
  • Nooruddin Lakhani
    Nooruddin Lakhani over 8 years
    Great answer (Y) Thanks
  • Sheraz Ahmad Khilji
    Sheraz Ahmad Khilji about 8 years
    For me this worked partially but in order to enable/disable toolbar scrolling i followed the following answer
  • Yasin Kaçmaz
    Yasin Kaçmaz almost 8 years
    If anyone want disable scrollflags programmatically use this : params.setScrollFlags(0);
  • Filipe Brito
    Filipe Brito over 7 years
    +ianhanniballake I think that is need to change the scrolling flags in some cases as I described here: stackoverflow.com/q/39462310/4744263 Do you agree?
  • Chaos
    Chaos about 7 years
    After support 25.3.1, it need to invoke appBarLayout.requestLayout() to take effects.
  • ianhanniballake
    ianhanniballake about 7 years
    @Chaos - that shouldn't be necessary. If you have a sample project that works on an older version of the Support Library but doesn't in 25.3.1, please file a bug
  • mrid
    mrid over 6 years
    How to remove these flags ??
  • reVerse
    reVerse almost 6 years
    @Chaos seems to be right. You have to call #setLayoutParams(...) on your view (which implicitly calls requestLayout()) or call it directly. Otherwise the changes won't have any effect.
  • itabdullah
    itabdullah over 4 years
    Need to add toolbar.setLayoutParams(params). setLayoutParams on toolbar is necessary for the changes to take effect especially when switching between setting and clearing scrollflags