Mecid
Mecid

Reputation: 4551

layout_scrollFlags not working for views outside CollapsingToolbarLayout

I want to scroll out user description but layout_scrollFlags="scroll" not working for RelativeLayout. ViewPager has 2 fragments with SwipeRefreshLayout and RecyclerView inside. How can i scroll out description while CollapsingToolbar exitUntilCollapsed?

<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/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="wrap_content"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginBottom="@dimen/keyline_1">

            <ImageView
                android:id="@+id/avatar"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginLeft="@dimen/keyline_1"
                android:layout_marginStart="@dimen/keyline_1" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/keyline_1"
                android:layout_marginStart="@dimen/keyline_1"
                android:layout_toEndOf="@id/avatar"
                android:layout_toRightOf="@id/avatar"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/full_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body2" />

                <TextView
                    android:id="@+id/screen_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

            </LinearLayout>

        </RelativeLayout>

        <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>

    <RelativeLayout
        android:id="@+id/information"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll">

        <TextView
            android:id="@+id/bio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="@dimen/keyline_1"
            android:layout_marginLeft="@dimen/keyline_3"
            android:layout_marginRight="@dimen/keyline_1"
            android:layout_marginStart="@dimen/keyline_3"
            android:layout_marginTop="8dp"
            android:textAppearance="@style/TextAppearance.AppCompat.Caption" />

    </RelativeLayout>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabContentStart="@dimen/keyline_3"
        app:tabMode="scrollable" />

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

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

Upvotes: 2

Views: 7179

Answers (1)

anandbibek
anandbibek

Reputation: 2594

Scroll flags won't work on any container unless that component is designed to do so. Specific components like AppBarLayout are only designed like that.

CoordinatorLayout works by searching through any child view that has a CoordinatorLayout Behavior defined either statically as XML with a app:layout_behavior tag or programmatically with the View class annotated with the @DefaultBehavior decorator. When a scroll event happens, CoordinatorLayout attempts to trigger other child views that are declared as dependencies.

To define your own a CoordinatorLayout Behavior, the layoutDependsOn() and onDependentViewChanged() should be implemented. For instance, AppBarLayout.Behavior has these two key methods defined. This behavior is used to trigger a change on the AppBarLayout when a scroll event happens.

The best way to understand how to implement these custom behaviors is by studying the AppBarLayout.Behavior and FloatingActionButtion.Behavior examples. Although the source code is not publicly available yet, you can use the decompiler integrated with Android Studio 1.2 to examine how they work by navigating up the source tree.

You can see an example here

Upvotes: 7

Related Questions