Search code examples
androidandroid-coordinatorlayoutandroid-collapsingtoolbarlayoutcoordinator-layoutnestedlayout

Collapsing layout during scrolling in Android


I would like to have custom view collapsed with a transition during scrolling.

I have AppBarLayout with a Toolbar inside. Below that there is a custom view that I want to collapse.

Below custom view there is a NestedScrollView with LinearLayout.

Toolbar is green, custom layout is pink and scroll with linear is gray:

toolbar custom view and

After scrolling down:

scrolled down

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

        <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" />
    </android.support.design.widget.AppBarLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:layout_marginTop="?attr/actionBarSize"
        android:background="@drawable/background"
        android:gravity="center">
    </RelativeLayout>

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="170dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <include
            layout="@layout/linear"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </ScrollView>

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

Should I go with custom behavior and CoordinatorLayout or translation with NestedScroll with translation animation?


Solution

  • I've managed to do this without any library. The key was to have two app bar layouts - one with a placeholder and collapsing toolbar layout that was hidden under custom view, and the second normal one.

    Then I've created two behaviors - one for changing nested layout height and second to manipulate inner views inside custom view.

    Here is my layout:

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:titleEnabled="false">
    
            <View
                android:id="@+id/vieItemDetailsPlaceholder"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#00000000" />
    
        </android.support.design.widget.CollapsingToolbarLayout>
    
    </android.support.design.widget.AppBarLayout>
    
    <android.support.design.widget.AppBarLayout
        android:id="@+id/second_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/second_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.widget.NestedScrollView
        android:id="@+id/detailContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.example.detail.view.Det ailScrollBehavior">
    
        <include
            layout="@layout/content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp" />
    
    </android.support.v4.widget.NestedScrollView>
    
    <!-- second layout with behavior -->
    <include layout="@layout/two_circles_layout" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    DetailScrollBehavior:

    public class DetailScrollBehavior extends Behavior<NestedScrollView> {
    
        private final Context context;
    
        public DetailScrollBehavior(Context context, AttributeSet attrs) {
            this.context = context;
        }
    
        @Override
        public boolean layoutDependsOn(CoordinatorLayout parent, NestedScrollView child, View dependency) {
            return dependency.getId() != R.id.ablItemDetailsSecondToolbar && dependency instanceof AppBarLayout;
        }
    
        @Override
        public boolean onDependentViewChanged(CoordinatorLayout parent, NestedScrollView child, View dependency) {
            int minHeight = context.getResources().getDimensionPixelSize(R.dimen.some_min_height);
            int placeholderHeight = getPlaceholderHeight(dependency);
            int actionBarHeight = getActionBarHeight(context.getTheme());
    
            if (placeholderHeight < minHeight + actionBarHeight) {
                placeholderHeight = minHeight + actionBarHeight;
            }
    
            child.setPadding(0, placeholderHeight, 0, 0);
    
            return true;
        }
    }
    

    and part of two_circles_layout:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/relDetailsContainer"
        android:layout_width="match_parent"
        android:layout_height="@dimen/team_vs_details_height"
        android:layout_marginTop="?attr/actionBarSize"
        android:background="@drawable/background"
        android:gravity="center"
        app:layout_behavior="com.example.DetailBehavior"
        tools:showIn="@layout/activity_item_detail">
    
    
        [ ... ]
    
    </RelativeLayout>