CollapsingToolbarLayout should not break when RecyclerView is empty

I use the following layout in my project. It works fine, but CollapsingToolbarLayout is crashing even if RecyclerView is empty or RecyclerView has very few items. The behavior I want, CollapsingToolbarLayout should only break when the RecyclerView has elements that are larger than the visible elements. How can I achieve this behavior?

<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout android:id="@+id/coordinator_layout" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true"> <android.support.design.widget.AppBarLayout android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <include layout="@layout/header" android:fitsSystemWindows="true" app:layout_collapseMode="parallax"/> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </android.support.design.widget.CoordinatorLayout> 
+7
android android-recyclerview collapsingtoolbarlayout androiddesignsupport
source share
3 answers

His famous mistake. Update your support libraries to 22..2.1:

 com.android.support:design:22.2.1 
0
source share

If this is still relevant, I think I managed to achieve the desired behavior.

First of all, to enable / disable scrolling, we need to implement a custom LinearLayoutManager (thanks to this post ):

 public class CustomLayoutManager extends LinearLayoutManager { private boolean isScrollEnabled = true; public CustomLayoutManager(Context context) { super(context); } public void setScrollEnabled(boolean flag) { this.isScrollEnabled = flag; } @Override public boolean canScrollVertically() { return isScrollEnabled && super.canScrollVertically(); } } 

and set it to RecyclerView :

 layoutManager = new CustomLayoutManager(this); recyclerView.setLayoutManager(layoutManager); 

Then we need to determine when to enable and disable scrolling. I did this using AppBarLayout.OnOffsetChangedListener :

 appBarLayout = mainView.findViewById(R.id.app_bar_layout); appBarLayout .addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { // verticalOffset == 0 means that the AppBarLayout is expanded if (verticalOffset == 0) { // here we check that the last item of the RecyclerView is visible if (viewIsVisible(layoutManager.findViewByPosition(layoutManager.getItemCount() - 1))) { layoutManager.setScrollEnabled(false); } else { layoutManager.setScrollEnabled(true); } } } }); 

And here is a view visibility test method:

 private boolean viewIsVisible(View view) { Rect scrollBounds = new Rect(); list.getHitRect(scrollBounds); return view != null && view.getLocalVisibleRect(scrollBounds); } 
0
source share

Try changing your collapsingBarLayout xml to:

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

I added two more flags: enterAlways and enterAlwaysCollapsed. This is not a 100% working solution, but better than the behavior you currently have.

-2
source share

All Articles