Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CollapsingToolbarLayout not scrolling with scrollview

So to get to this point, I've followed this tutorial: http://blog.grafixartist.com/parallax-scrolling-tabs-design-support-library/

In the tutorial, a viewpager that is given fragments with a RecyclerView is used. I'm trying to give the ViewPager fragments with a Scrollview containing a LinearLayout instead.

My ViewPager and TabLayouts are working properly. The problem is, I don't think the CollapsingToolbarLayout is catching the scrolling behavior of the ScrollViews. I can collapse the toolbar by scrolling it up, and I can scroll in my scrollview, but the motions are not linked as they should be - Isn't the CoordinatorLayout supposed to take care of this? Maybe this is setup improperly? If anyone spots something please let me know - thanks ahead of time!!

Here is the layout code I'm using:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/htab_maincontent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/htab_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

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

            <android.support.v7.widget.Toolbar
                android:id="@+id/htab_toolbar"
                android:layout_width="match_parent"
                android:layout_height="104dp"
                android:gravity="top"
                android:minHeight="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:titleMarginTop="13dp" />

            <android.support.design.widget.TabLayout
                android:id="@+id/htab_tabs"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="bottom"
                app:tabIndicatorColor="@android:color/white" />

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

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

    <android.support.v4.view.ViewPager
        android:id="@+id/view_object_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

And here is the fragment that I set my ViewPager up with (both tabs are an instance of the same fragment for testing):

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".objectDetailsFragment"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:orientation="vertical">

        ...

    </android.support.v7.widget.LinearLayoutCompat>
</ScrollView>
like image 206
Riptyde4 Avatar asked Mar 16 '16 19:03

Riptyde4


1 Answers

ScrollView doesn't work like this instead use NestedScrollView:

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".objectDetailsFragment"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:orientation="vertical">

        ...

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

and add dependencies:

 dependencies{
     compile 'com.android.support:design:23.2.0'
 }
like image 159
kandroidj Avatar answered Oct 01 '22 23:10

kandroidj