android - 拦截RecyclerView在NestedScrollView中向下滑动

标签 android android-recyclerview android-coordinatorlayout android-nestedscrollview

我有以下布局:

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

fixedBannercard1 向下滚动(手指向上)时,appBarLayout 首先折叠,然后 nestedScrollView > 向下滚动。但是,如果从 recyclerView 向下滚动,recyclerView 就会开始滚动。我希望 nestedScrollViewrecyclerView 之前先向下滚动。

我尝试使用在 nestedScrollView 上设置的自定义 CardViewBehavior ,如果 appBarLayout 未完全折叠且有,它会覆盖 onNestedPreScroll 以消耗滚动增量仍然可以在 nestedScrollView 中滚动。

但是,如果我在 recyclerView 上滑动得足够快,recyclerView 就会在 nestedScrollView 完全滚动到底部之前开始滑动。我尝试在 CardViewBehavior 中重写 onNestedPreFlingonNestedFling,但是当 RecyclerView 开始自行 fling 时,这两个方法似乎从未被调用。

如何确保 nestedScrollViewrecyclerView 开始滚动之前滚动到底部?

最佳答案

扩展NestedScrollView并直接重写onNestedPreScroll方法。

(在 https://www.androiddesignpatterns.com/2018/01/experimenting-with-nested-scrolling.html 上找到解决方案)

关于android - 拦截RecyclerView在NestedScrollView中向下滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56446819/

相关文章:

android - 无法从RecyclerView更新BottomSheet上的数据

android - 在另一个 Fragment 中添加新数据后,在 Activity 中刷新 RecyclerView

android - 如何使用 NestedScrollView 删除 CoordinatorLayout 底部的空白区域

android - 如何恢复使用 SwipeDismissBehavior 关闭的 View

android - 为什么 NestedScrollView 在 CollapsingToolbarLayout 完全折叠时停止滚动?

android - 使用 iScroll 会阻止键盘显示在我的设备上

java - 如何在android中检查JobService是否正在运行?

android - 如何通知数据从 sqlite 更改为回收站 View ?

android - 库模块中的数据绑定(bind)

android - Jenkins-CI 没有正确签署 apk