Reputation: 5597
I'm using single Activity
approach with multiple fragments, in the main screen I have RecycleView
and I want to hide Toolbar when scrolling only in the main screen, since it's single activity and one top level CoordinatorLayout
the Toolbar
hides when scrolling in all screens.
How to enable "hide toolbar on scroll" in some screens and disable it for others in single activity?
Upvotes: 1
Views: 558
Reputation: 5597
@Yonatan's answer in my case was not enough, when navigating between fragments some views were not visible in the bottom, probably some height misconfiguration.
Enabling and disabling scroll flags for Toolbar
works just fine.
public void disableToolbarScrollBehavior() {
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(0);
}
public void enableToolbarScrollBehavior() {
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
}
Upvotes: 1
Reputation: 160
You gotta trace which fragment(screen) is active in your activity and use these functions to hide or show.
fun enableLayoutBehaviour() {
val layoutParams: CoordinatorLayout.LayoutParams = coordinatorLayout.layoutParams
layoutParams.behavior = AppBarLayout.ScrollingViewBehavior()
}
fun disableLayoutBehaviour() {
val layoutParams: CoordinatorLayout.LayoutParams = coordinatorLayout.layoutParams
layoutParams.behavior = null
}
Upvotes: 2