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?
Asked
Active
Viewed 3.6k times
3 Answers
143
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)

thanhbinh84
- 17,876
- 6
- 62
- 69

ianhanniballake
- 191,609
- 30
- 470
- 443
-
33Thank you. I don't want to make it that way but my stupid client want it :D. – Hoang Ha Jun 11 '15 at 08:14
-
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? – android developer Aug 05 '15 at 14:34
-
2It does make sense to play with scroll flags in specific scenarios like when entering search mode (Toolbar's ActionMode) – marmor Aug 17 '15 at 06:41
-
For me this worked partially but in order to enable/disable toolbar scrolling i followed the following [answer](http://stackoverflow.com/a/31167852/1118886) – Sheraz Ahmad Khilji May 04 '16 at 16:37
-
If anyone want disable scrollflags programmatically use this : `params.setScrollFlags(0);` – Yasin Kaçmaz Jul 19 '16 at 17:51
-
+ianhanniballake I think that is need to change the scrolling flags in some cases as I described here: http://stackoverflow.com/q/39462310/4744263 Do you agree? – Filipe Brito Sep 14 '16 at 03:43
-
5After support 25.3.1, it need to invoke `appBarLayout.requestLayout()` to take effects. – Chaos May 02 '17 at 13:31
-
@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](https://issuetracker.google.com/issues/new?component=192731&template=842428) – ianhanniballake May 02 '17 at 17:19
-
How to remove these flags ?? – mrid Sep 13 '17 at 06:33
-
3@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. – reVerse Jul 19 '18 at 15:36
-
1Need to add `toolbar.setLayoutParams(params)`. setLayoutParams on toolbar is necessary for the changes to take effect especially when switching between setting and clearing scrollflags – itabdullah Nov 26 '19 at 09:50
15
// 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);
}

Denny Weinberg
- 2,492
- 1
- 21
- 34
0
Kotlin version for easy copy&paste. Should work with core-ktx-1.8.0
lib
private fun SomeLayoutBinding.setScrollBehavior(enabled: Boolean) {
appBarLayoutParams.updateLayoutParams<AppBarLayout.LayoutParams> {
scrollFlags =
if (enabled) AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL or AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS
else 0
}
}

George Shalvashvili
- 1,263
- 13
- 21