12

I am using following layout in my project.Its working fine but CollapsingToolbarLayout is collapsing even when RecyclerView is empty or RecyclerView has very few items. Behaviour that i want is, CollapsingToolbarLayout should collapse only when RecyclerView has items greater than visible items. How can I achieve this behaviour?

<?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>
Vivart
  • 14,900
  • 6
  • 36
  • 74
  • Hi Vivart, Me to facing same issue. If you have fixed issue, please share your idea. Thanks – Murali Ganesan Aug 04 '15 at 11:43
  • i also implemented the same but unfortunately my collapsing barlayout isnt collapsing. ever. look at my question - http://stackoverflow.com/questions/33093066/collapsing-bar-layout-and-recyclerview – Aman Verma Oct 13 '15 at 03:38

4 Answers4

0

Its a known bug. Update your Support libraries to 22..2.1:

com.android.support:design:22.2.1
Saadi
  • 1,292
  • 13
  • 22
0

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

First of all, to enable/disable scrolling, we have 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 the RecyclerView:

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

Then we need to detect, when to enable and when to disable scrolling. I did it with an 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 the method to check if the view is visible:

private boolean viewIsVisible(View view) {
    Rect scrollBounds = new Rect();
    list.getHitRect(scrollBounds);
    return view != null && view.getLocalVisibleRect(scrollBounds);
}
Kirill Simonov
  • 8,257
  • 3
  • 18
  • 42
0
<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.v4.widget.NestedScrollView
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="match_parent">

 <android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:nestedScrollingEnabled="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

  </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>
Praveen
  • 946
  • 6
  • 14
  • android:nestedScrollingEnabled="false" is compulsory or recyclerview.setNestedScrollingEnabled(false); – Praveen Jun 20 '18 at 04:40
-1

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 have added 2 additional flags : enterAlways and enterAlwaysCollapsed. This is not a 100% working solution but is better then the behaviour you currently have.

mudit
  • 25,306
  • 32
  • 90
  • 132