Android NestedScrollView嵌套RecyclerView滑动卡顿问题终极解决方案

如果是如下的页面布局:NestedScrollView嵌套RecyclerView
在这里插入图片描述
随着recyclerVIew的加载,item越多,会发现滑动卡顿;大致解决方案有以下两种:

方案1:取消recyclerVIew的滑动

	mRecyclerView.setNestedScrollingEnabled(false);

这种方案可能解决绝大部分问题。但是如果item中是加载的图片。但是可能就不起作用了。
可以尝试方案二;

方案2:采用 CoordinatorLayout + AppBarLayout解决

即用AppBarLayout包裹列表以外需要滑动的view。这样就可以实现联动了;注意此时一定不能设置

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
今日推荐