Pavel Poley
Pavel Poley

Reputation: 5597

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?

Upvotes: 1

Views: 558

Answers (2)

Pavel Poley
Pavel Poley

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

Yonatan
Yonatan

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

Related Questions