简体   繁体   中英

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.

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.

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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM