'Intercepting RecyclerView downwards fling in NestedScrollView

I have the following layout:

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/rootLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="160dp"
                    app:layout_collapseMode="parallax" />

                <android.support.v7.widget.Toolbar
                    android:layout_width="match_parent"
                    android:layout_height="52dp"
                    app:layout_collapseMode="pin" />
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            app:layout_behavior="com.example.CardViewBehavior">

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

                <FrameLayout
                    android:id="@+id/fixedBanner"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                </FrameLayout>

                <FrameLayout
                    android:id="@+id/card1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <!-- random content -->
                </FrameLayout>

                <FrameLayout
                    android:id="@+id/card2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/recyclerView"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </FrameLayout>
            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>
</layout>

When scrolling down (finger goes upwards) from fixedBanner or card1, the appBarLayout collapses first, then nestedScrollView scrolls down. However, if scrolling down from recyclerView, recyclerView starts scrolling. I want nestedScrollView to scroll down first before recyclerView.

I have tried using a custom CardViewBehavior set on nestedScrollView which overrides onNestedPreScroll to consume scroll deltas if appBarLayout is not fully collapsed and there is still range to scroll in nestedScrollView.

However, if I swipe fast enough on recyclerView, recyclerView starts flinging before nestedScrollView has fully scrolled to bottom. I tried overriding onNestedPreFling and onNestedFling in CardViewBehavior, but it seems those two methods were never called when RecyclerView starts flinging by itself.

How can I ensure that nestedScrollView scrolls to bottom before recyclerView starts scrolling?



Solution 1:[1]

Extend NestedScrollView and override the onNestedPreScroll method directly.

(Solution was found on https://www.androiddesignpatterns.com/2018/01/experimenting-with-nested-scrolling.html)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1