Prevent CollapsingToolbarLayout collapse if not needed
Asked Answered
B

6

39

Using:

compile 'com.android.support:design:23.0.0'
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:cardview-v7:23.0.0'
compile 'com.android.support:recyclerview-v7:23.0.0'

With the project Cheesesquare updated.

Into the detail of cheese, I remove 2 cards (to have only one). Is there a way to prevent the collapsing of the toolbar that show a blank space?

enter image description here

Butt answered 29/8, 2015 at 16:22 Comment(3)
I didn't really find a solution, however I'm now using github.com/henrytao-me/smooth-app-bar-layout and I don't have this issue (since the implementation is different)Butt
what did you do about it ?Margalo
This is what you need custom scrolling view behaviorUnmanned
S
37

To implement such behaviour in Cheesesquare example just modify android:layout_height param of the NestedScrollView to wrap_content. It will prevent scrolling by content if it is small enough to fit on the screen.

And to prevent scrolling by CollapsingToolbarLayout you should programmatically set layout_scrollFlags parameter to the AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP value.

Here described how you can do this.

Subtropical answered 19/10, 2015 at 14:40 Comment(4)
Or in xml add app:layout_scrollFlags="snap" to the CollapsingToolbarLayout tagZulmazulu
then nested scrollview is good but the collapse toolbar is still scrollableAmphibolite
layout_scrollFlags is not working. use this workaround for disable CollapsingToolbarLayout scrolling. https://mcmap.net/q/50113/-disable-vertical-scroll-in-collapsingtoolbarlayout-appbarlayout it is true way and really works! )Friedlander
In my case, wrap_content resolves the problem. The layout has AppBarLayout and NestedScrollView.Jiggered
H
5

You can use below code for this:

   public static void stopScroll() {
    AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) collapsing_toolbar.getLayoutParams();
    toolbarLayoutParams.setScrollFlags(0);
    collapsing_toolbar.setLayoutParams(toolbarLayoutParams);

    CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
    appBarLayoutParams.setBehavior(null);
    appbar.setLayoutParams(appBarLayoutParams);
}

public static void startScroll() {
    AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) collapsing_toolbar.getLayoutParams();
    toolbarLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
    collapsing_toolbar.setLayoutParams(toolbarLayoutParams);

    CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
    appBarLayoutParams.setBehavior(new AppBarLayout.Behavior());
    appbar.setLayoutParams(appBarLayoutParams);
}
Harp answered 8/10, 2018 at 10:37 Comment(1)
Works for me as I wanted!Shall
S
2

In xml I have used property

app:layout_scrollFlags="snap" in <android.support.design.widget.CollapsingToolbarLayout

and following in the activity

 toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
 toolbar.setTitle(null);
 toolbar.setCollapsible(false);

It is working now.

Stopover answered 28/2, 2016 at 5:59 Comment(0)
T
2

A data-binding solution inspired by @Vishal's answer

    <com.google.android.material.appbar.AppBarLayout>
        <com.google.android.material.appbar.CollapsingToolbarLayout
            app:enableCollapsingScroll="@{listItems.size > 0}"
            ... />
    </com.google.android.material.appbar.AppBarLayout>
    @BindingAdapter("app:enableCollapsingScroll")
    fun setCollapsingToolbarLayoutScrollEnabled(collapsingToolbarLayout: CollapsingToolbarLayout, enabled: Boolean?) {
        val lp = collapsingToolbarLayout.layoutParams as AppBarLayout.LayoutParams
        if (enabled.orFalse()) {
            lp.scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL or AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED
        } else {
            lp.scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP
        }
        collapsingToolbarLayout.layoutParams = lp
    }
Tims answered 18/6, 2019 at 0:43 Comment(0)
K
0

Here is my working code, to initially collapes the bar:

_appbar.setExpanded(false);

   AppBarLayout _appbar = (AppBarLayout) findViewById(R.id.appbar);
    _appbar.setExpanded(false);

here is the layout xml

 <android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/detail_backdrop_height"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp">



        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

            app:layout_collapseMode="pin" />





    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

the reference is: AppBarLayout.setExpanded(boolean)

Kilovolt answered 15/7, 2016 at 18:38 Comment(1)
The question is how to prevent collapsingSchlueter
L
0
  AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) activityUserGroupProfleBinding.collapsingToolbarLayout.getLayoutParams();
        if (logic) {
            params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
        } else {
            params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP);
        }
        activityUserGroupProfleBinding.collapsingToolbarLayout.setLayoutParams(params);
Lebel answered 4/7, 2019 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.