Prevent CollapsingToolbarLayout collapse if not needed
Solution 1
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.
Solution 2
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);
}
Solution 3
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.
Solution 4
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
}
stankocken
Updated on August 17, 2020Comments
-
stankocken over 3 years
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?