Search code examples
androidandroid-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.

How to enable "hide toolbar on scroll" in some screens and disable it for others in single activity?


Solution

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