Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock CollapsingToolbarLayout from Android support library

I am using Activity class with (usually) one fragment as a content. In the Activity I use CollapsingToolbarLayout as some kind of header for some information and everything works fine. But in some cases (when some fragments are attached) I don't want to show that info, I don't want CollapsingToolbarLayout to open on scroll.

What I want to achieve is to lock CollapsingToolbarLayout, prevent it from opening from the fragment. I am collapsing it programmatically with appBarLayout.setExpanded(false, true);

like image 552
urgas9 Avatar asked Jan 06 '16 14:01

urgas9


People also ask

How do I stop my Toolbar from collapsing?

Solution: The solution is simple, we just need to set the app:scrimAnimationDuration=”0" in our collapsing toolbar layout like the below code snippet. Now just run the code and see the results, you will see then there will be no fading animation anymore.

What is contentScrim in Android?

app:contentScrim: Specifies drawable or Color value when scrolled sufficiently off screen. app:layout_collapseMode: Specifies how child views of collapsing toolbar layout move when layout is moving. Parallax- moves in parallax fashion, Pin-view placed in fixed position.

What is CollapsingToolbarLayout?

Android CollapsingToolbarLayout is a wrapper for Toolbar which implements a collapsing app bar. It is designed to be used as a direct child of a AppBarLayout. This type of layout is commonly seen in the Profile Screen of the Whatsapp Application.


2 Answers

I came up with a different method as setting the nested scrolling flag only works when dragging the NestedScrollView. The appbar can still be expanded by swiping on the bar itself.

I set this up as a static function in "Utils" class. Obviously the flags you set upon unlocking will depend on which ones are relevant for your use case.

This function assumes you are are starting with an expanded toolbar

public static void LockToolbar(boolean locked, final AppBarLayout appbar, final CollapsingToolbarLayout toolbar) {

    if (locked) {
        // We want to lock so add the listener and collapse the toolbar
        appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {

            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (toolbar.getHeight() + verticalOffset < 2 * ViewCompat.getMinimumHeight(toolbar)) {
                    // Now fully expanded again so remove the listener
                    appbar.removeOnOffsetChangedListener(this);
                } else {
                    // Fully collapsed so set the flags to lock the toolbar
                    AppBarLayout.LayoutParams lp = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
                    lp.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED);
                }
            }
        });
        appbar.setExpanded(false, true);
    } else {
        // Unlock by restoring the flags and then expand 
        AppBarLayout.LayoutParams lp = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
        lp.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
        appbar.setExpanded(true, true);
    }

}
like image 82
Kuffs Avatar answered Nov 01 '22 13:11

Kuffs


Well, I managed to solve it myself. The trick is to disable nested scrolling behaviour with ViewCompat.setNestedScrollingEnabled(recyclerView, expanded);

As I am using one fragment in the activity as a content view and putting it on the backstack I simply check when backstack has changed and which fragment is visibile. Note that I NestedScrollView in every fragment to trigger collapsible toolbar. This is my code:

getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            NestedScrollView nestedScrollView = (NestedScrollView)findViewById(R.id.nested_scroll_view);
            int size = getSupportFragmentManager().getBackStackEntryCount();
            if (size >= 1 && nestedScrollView != null) {
                if (getSupportFragmentManager().getBackStackEntryAt(size - 1).getName().equals("SpotDetailsFragment")) {
                    Log.d(LOG_TAG, "Enabling collapsible toolbar.");
                    ViewCompat.setNestedScrollingEnabled(nestedScrollView, true);
                } else {
                    Log.d(LOG_TAG, "Disabling collapsible toolbar.");
                    ViewCompat.setNestedScrollingEnabled(nestedScrollView, false);
                }
            }
        }
    });

This thread helped me a lot, where another possible solution is presented: Need to disable expand on CollapsingToolbarLayout for certain fragments

like image 22
urgas9 Avatar answered Nov 01 '22 13:11

urgas9