MrHappy
MrHappy

Reputation: 47

CollapsingToolbarLayout & NestedScrollView don't work together

I'm trying to make CollapsingToolbarLayout work with NestedScrollView, but it doesn't follow the scrolling of NestedScrollView correctly. It moves a little bit when the NestedScrollView reaches the top or bottom end, but that's clearly not the intended behavior.

Oh, and the contentScrim covers the ImageView immediately as well.

Here's a video of what happens: https://youtu.be/1GlTJq5fd0U

And here's the xml:

<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:id="@+id/articleLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.gmail.senokt16.bosphoruschronicle.ArticleActivity">


    <android.support.design.widget.AppBarLayout
        android:id="@+id/articleAppBarLayout"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/articleCollapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/primaryColor"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/article_app_bar">

            <ImageView
                android:id="@+id/articleActivityImage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax" />

            <include
                android:id="@+id/article_app_bar"
                layout="@layout/article_app_bar" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:fitsSystemWindows="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_scrollFlags="scroll|enterAlways">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="24dp">

            <TextView
                android:id="@+id/articleActivityContents"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="30dp"
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:paddingTop="5dp" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/articleFAB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@android:drawable/ic_menu_share"
        app:layout_anchor="@id/articleAppBarLayout"
        app:layout_anchorGravity="bottom|right|end" />


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

Upvotes: 0

Views: 2127

Answers (1)

MrHappy
MrHappy

Reputation: 47

I ended up playing around with a bunch of xml tags and don't know which one fixed the problem, but here's the last version of the xml:

<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:id="@+id/articleLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.gmail.senokt16.bosphoruschronicle.ArticleActivity">


    <android.support.design.widget.AppBarLayout
        android:id="@+id/articleAppBarLayout"
        android:layout_width="match_parent"
        android:layout_height="280dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/articleCollapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/primaryColor"
            app:expandedTitleMarginEnd="96dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <View
                android:layout_width="match_parent"
                android:fitsSystemWindows="true"
                android:layout_height="match_parent"
                android:background="@drawable/gradient"
                />
            <ImageView
                android:id="@+id/articleActivityImage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"
                android:scaleType="centerCrop"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/article_app_bar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="24dp">

            <TextView
                android:id="@+id/articleActivityContents"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="30dp"
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:paddingTop="5dp" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/articleFAB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@android:drawable/ic_menu_share"
        app:layout_anchor="@id/articleAppBarLayout"
        app:layout_anchorGravity="bottom|right|end" />


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

Upvotes: 2

Related Questions