40

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

3

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Or in xml add app:layout_scrollFlags="snap" to the CollapsingToolbarLayout tag
then nested scrollview is good but the collapse toolbar is still scrollable
layout_scrollFlags is not working. use this workaround for disable CollapsingToolbarLayout scrolling. stackoverflow.com/a/40750707/3094065 it is true way and really works! )
In my case, wrap_content resolves the problem. The layout has AppBarLayout and NestedScrollView.
6

You can use below code for this:

public static void stopScroll() { AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) collapsingToolbar.getLayoutParams(); toolbarLayoutParams.setScrollFlags(0); collapsingToolbar.setLayoutParams(toolbarLayoutParams); CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams(); appBarLayoutParams.setBehavior(null); appbar.setLayoutParams(appBarLayoutParams); } public static void startScroll() { AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) collapsingToolbar.getLayoutParams(); toolbarLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); collapsingToolbar.setLayoutParams(toolbarLayoutParams); CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams(); appBarLayoutParams.setBehavior(new AppBarLayout.Behavior()); appbar.setLayoutParams(appBarLayoutParams); } 

1 Comment

Works for me as I wanted!
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 } 

Comments

1

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.

Comments

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

Comments

-1

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

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)

1 Comment

The question is how to prevent collapsing

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.