Android NestedScrollView のネストされた RecyclerView のスライド フリーズ問題に対する究極の解決策

次のページ レイアウトの場合: NestedScrollView ネストされた RecyclerView recyclerVIew が
ここに画像の説明を挿入
読み込まれるにつれて、項目が増えると、スライドがスタックすることがわかります。一般的な解決策は 2 つあります。

解決策 1:recyclerVIew のスライドをキャンセルする

	mRecyclerView.setNestedScrollingEnabled(false);

この解決策により、ほとんどの問題が解決される可能性があります。ただし、アイテムに読み込まれた画像がある場合。しかし、うまくいかないかもしれません。
オプション 2 を試すことができます。

解決策 2: CoordinatorLayout + AppBarLayout を使用して解決する

つまり、AppBarLayout を使用して、リストの外側にスライドする必要があるビューをラップします。このようにして連携を実現しますが、この時点では設定しないでください。

mRecyclerView.setNestedScrollingEnabled(false);

コアコードは次のとおりです。

 <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:id="@+id/layout_data"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:overScrollMode="never"
            android:scrollbars="none">

            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/mAppBarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@null"
                android:fadingEdge="none"
                app:elevation="0dp">

                <androidx.core.widget.NestedScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:overScrollMode="never"
                    android:scrollbars="none"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">

                    <ImageView
                        android:id="@+id/iv_top"
                        android:layout_width="match_parent"
                        android:layout_height="76dp"
                        android:scaleType="fitXY"
                        android:src="@mipmap/ic_estimate_title" />


                </androidx.core.widget.NestedScrollView>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="0dp"/>

            </com.google.android.material.appbar.AppBarLayout>

            <RelativeLayout
                android:id="@+id/relativeLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="-8dp"
                    android:layout_marginRight="-8dp"
                    android:background="@color/color_bg"
                    android:divider="@color/trans"
                    android:horizontalSpacing="0dp"
                    android:listSelector="@color/trans"
                    android:overScrollMode="never"
                    android:scrollbars="none"
                    android:verticalSpacing="12dp" />

            </RelativeLayout>


        </androidx.coordinatorlayout.widget.CoordinatorLayout>

これで完了です。

おすすめ

転載: blog.csdn.net/mingtiannihao0522/article/details/129322330