How to set app:layout_scrollFlags for Toolbar programmatically
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);
}
Hoang Ha
Updated on October 01, 2020Comments
-
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 almost 9 yearsThank you. I don't want to make it that way but my stupid client want it :D.
-
android developer almost 9 yearsIs 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 almost 9 yearsIt does make sense to play with scroll flags in specific scenarios like when entering search mode (Toolbar's ActionMode)
-
Nooruddin Lakhani over 8 yearsGreat answer (Y) Thanks
-
Sheraz Ahmad Khilji about 8 yearsFor me this worked partially but in order to enable/disable toolbar scrolling i followed the following answer
-
Yasin Kaçmaz almost 8 yearsIf anyone want disable scrollflags programmatically use this :
params.setScrollFlags(0);
-
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 about 7 yearsAfter support 25.3.1, it need to invoke
appBarLayout.requestLayout()
to take effects. -
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 over 6 yearsHow to remove these flags ??
-
reVerse almost 6 years@Chaos seems to be right. You have to call
#setLayoutParams(...)
on your view (which implicitly callsrequestLayout()
) or call it directly. Otherwise the changes won't have any effect. -
itabdullah over 4 yearsNeed to add
toolbar.setLayoutParams(params)
. setLayoutParams on toolbar is necessary for the changes to take effect especially when switching between setting and clearing scrollflags