简体   繁体   English

仅在特定片段中滚动时,CoordinatorLayout 隐藏工具栏

[英]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并且我想在仅在主屏幕中滚动时隐藏工具栏,因为它是单个活动和一个顶级CoordinatorLayoutToolbar在所有屏幕中滚动时隐藏。

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.

相关问题 使用CoordinatorLayout隐藏工具栏,但在片段上使用RecyclerView - Hide Toolbar with CoordinatorLayout, but RecyclerView on a fragment Android CoordinatorLayout:仅在列表开始滚动时滚动工具栏 - Android CoordinatorLayout : ToolBar scroll only when list starts scrolling 在 Fragment 上滚动时如何隐藏工具栏 - How to hide toolbar when scrolling on Fragment 工具栏不会在CoordinatorLayout中滚动时隐藏/显示 - Toolbar does not Hide/Shown on Scrolling in CoordinatorLayout 使用CoordinatorLayout从片段中隐藏活动中的工具栏 - Hide Toolbar in activity from fragment using CoordinatorLayout 滚动时在CoordinatorLayout中隐藏工具栏和BottomBar-在透明状态栏下可见 - Hide Toolbar and BottomBar in CoordinatorLayout when scrolling - visible under transparent status bar CoordinatorLayout与工具栏和片段 - CoordinatorLayout with Toolbar and fragment 使用CoordinatorLayout滚动隐藏工具栏 - Hide Toolbar on scroll with CoordinatorLayout android在特定片段中隐藏工具栏 - android hide toolbar in specific fragment 滚动片段工具栏应隐藏在android中 - While Scrolling Fragment toolbar should hide in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM