Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CollapsingToolbarLayout and NestedScrollView not working

I am trying to implement CollapsingToolbarLayout with a NestedScrollView and it is displaying the TextView within the NestedScrollView at the bottom and not allowing, scrolling or collapsing the Toolbar. I have gotten this to work with a RecyclerView but not NestedScrollView. When I remove app:layout_behavior="@string/appbar_scrolling_view_behavior the Toolbar collapses but the NestedScrollView is not below the AppBarLayout. Any solutions or suggestions to fix this?

XML

<android.support.design.widget.CoordinatorLayout
    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="match_parent"
    android:fitsSystemWindows="true">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Hello"
                android:textColor="#000"
                android:textSize="16sp"/>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="134dp"
                android:background="@color/primary"
                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"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

Result

enter image description here

like image 392
Eugene H Avatar asked Jul 04 '15 17:07

Eugene H


People also ask

Why use Coordinator layout android?

As a container for a specific interaction with one or more child views. By specifying Behaviors for child views of a CoordinatorLayout you can provide many different interactions within a single parent and those views can also interact with one another.

What is collapsing toolbar Android?

CollapsingToolbarLayout is a wrapper for Toolbar which implements a collapsing app bar. It is designed to be used as a direct child of a AppBarLayout .

What is the difference between ScrollView and NestedScrollView in Android?

NestedScrollView is just like ScrollView , but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.


1 Answers

Change to some certain height in the AppbarLayout. Example:

android:layout_height="300dp". 

The main problem being, the nested scroll view does not have enough views to cause a scroll. Hence the parallax effect would not work.

Here is a working example that uses NestedScrollView and CollapsingToolbarLayout

like image 200
Psypher Avatar answered Oct 17 '22 04:10

Psypher