[英]CoordinatorLayout hide toolbar when scrolling only in specific fragment
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.我正在使用具有多个片段的单个Activity
方法,在主屏幕中我有RecycleView
并且我想在仅在主屏幕中滚动时隐藏工具栏,因为它是单个活动和一个顶级CoordinatorLayout
, Toolbar
在所有屏幕中滚动时隐藏。
How to enable "hide toolbar on scroll" in some screens and disable it for others in single activity?如何在某些屏幕中启用“滚动时隐藏工具栏”并在单个活动中为其他屏幕禁用它?
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
}
@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. @Yonatan 在我的情况下的回答还不够,在片段之间导航时,底部看不到一些视图,可能是一些高度配置错误。
Enabling and disabling scroll flags for Toolbar
works just fine.为Toolbar
启用和禁用滚动标志就可以了。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.