Happo
Happo

Reputation: 1397

AppBarLayout not collapsing during programmatically scroll

I have a ViewPager with Fragments which containe a RecyclerViews. A CollapsingTollbarLayout is above the ViewPager. Everythings works fine except during a programmatically scroll on the RecyclerView. Then the AppBarLayout or CollapsingToolbarLayout doesn't responde.

Here is my base layout xml:

<?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/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="256dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:layout_behavior="de.wackernagel.playball.ui.FlingBehaviour">

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

        <ImageView
            android:contentDescription="@null"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/showdown"
            app:layout_collapseMode="parallax"/>

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent"
            android:layout_gravity="bottom"
            app:tabTextColor="#BEFFFFFF"
            app:tabSelectedTextColor="#FFFFFFFF"
            app:tabContentStart="@dimen/keyline_2"
            app:tabMode="scrollable" />

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

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

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

My goal is it to collapse the CollapsingToolbarLayout during a programmatically scroll like a normal touch scroll.

I do this by

Activity get Fragment at current viewpager position
    Fragment get LayoutManager from RecyclerView
         LayoutManager scrollToPosition x

The scrolling behavior works on RecyclerView but the CollapsingToolbarLayout doesn't responde.

EDIT

Here is the layout of my fragments:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<de.wackernagel.playball.views.EmptyAwareRecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="8dp" />

<TextView
    android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"/>

Upvotes: 5

Views: 4163

Answers (3)

jwitt98
jwitt98

Reputation: 1255

I was having this same problem, the AppBarLayout wouldn't collapse when the NestedScrollView was programmatically scrolled to the bottom. This resulted in Android miscalculating the bottom of the NestedScrollView when the AppBarLayout was expanded. When it was collapsed, it would scroll to the bottom correctly, but when it was expanded, it would come short of scrolling to the bottom by an amount equal to the height of the AppBarLayout. I found a simple fix for this by setting setExpanded to false immediately before attempting to scroll.

//scroll to the end of NestedScrollView
AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout);
appBarLayout.setExpanded(false, true);
scrollView.post(() -> scrollView.fullScroll(View.FOCUS_DOWN));

...or if not using lambda expressions (i.e < Java 8)

//scroll to the end of NestedScrollView
AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout);
appBarLayout.setExpanded(false, true);
scrollView.post(new Runnable() {
    @Override
    public void run() {
        scrollView.fullScroll(View.FOCUS_DOWN);     
    }
});

Upvotes: 1

nikis
nikis

Reputation: 11234

It is quite easy to solve this problem, it comes from the fact, that getNestedScrollingParentForType(type) in NestedScrollingChildHelper#dispatchNestedScroll returns null (support lib version = 27.0.2) for the non-touch scroll, so scrolling is not dispatched. Thus, one should do the following before scrolling programmatically:

if (!recyclerView.hasNestedScrollingParent(ViewCompat.TYPE_NON_TOUCH)) {
    recyclerView.startNestedScroll(View.SCROLL_AXIS_VERTICAL, ViewCompat.TYPE_NON_TOUCH);
}

Upvotes: 5

Nima
Nima

Reputation: 1932

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

<android.support.design.widget.AppBarLayout
    android:id="@+id/htab_appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    app:layout_behavior="example.com.final.TabParallax.FlingBehavior"
    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="360dp"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="0dp">

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

        </FrameLayout>

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

    <android.support.design.widget.TabLayout
        android:id="@+id/htab_tabs"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorAccent1"
        android:fitsSystemWindows="true"
        android:gravity="bottom"
        app:tabGravity="center"
        app:tabIndicatorColor="@android:color/white"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@android:color/white"
        app:tabTextColor="@android:color/white"
        />
</android.support.design.widget.AppBarLayout>

Upvotes: -1

Related Questions