RecyclerView嵌套布局,导致RecyclerView复用失效 解决

前言:使用NestedScrollView嵌套RecyclerView。

解决步骤一:固定高度

NestedScrollView嵌套RecyclerView时,RecyclerView的高度是无限大,所以要将RecyclerView设置固定高度。在代码中固定的,灵活度更高。

java 复制代码
     binding.nestedScrollV.post(new Runnable() {
        @Override
        public void run() {
            binding.selectList.getLayoutParams().height = binding.nestedScrollV.getHeight(); // 使用NestedScrollView的高度
            binding.productList.getLayoutParams().height = binding.nestedScrollV.getHeight();                        
            binding.selectList.setLayoutParams(binding.selectList.getLayoutParams());  
            binding.productList.setLayoutParams(binding.productList.getLayoutParams());
     });

解决步骤二:重写NestedScrollView的 measureChildWithMargins**()**函数

java 复制代码
public class MNestedScrollViewBox extends NestedScrollView {

    public MNestedScrollViewBox(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    // 使用NestedScrollView嵌套RecyclerView,会导致RecyclerView复用机制失效,RecyclerView会将所有数据一次性全部加载。
    // 解决方法:重写measureChildWithMargins,让NestedScrollView测量RecyclerView时 不使用MeasureSpec.UNSPECIFIED模式即可。
    @Override
    protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
        child.measure(parentWidthMeasureSpec, parentHeightMeasureSpec);
    }

}

使用:

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<com.xx.xx.ui.widget.MNestedScrollViewBox xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_F5F6F7"
    android:fillViewport="true"
    android:overScrollMode="never"
    tools:context=".ui.fragment.shopdetail.OrderFoodFragment">

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

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/select_list"
            android:layout_width="84dp"
            android:layout_height="match_parent"
            android:overScrollMode="never"
            tools:listitem="@layout/widget_select_item_ho" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/product_list"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/white"
            android:overScrollMode="never"
            android:paddingStart="8dp"
            tools:ignore="RtlSymmetry"
            tools:listitem="@layout/widget_product_item_ho" />

    </LinearLayout>

</com.xx.xx.ui.widget.MNestedScrollViewBox>
相关推荐
AC赳赳老秦1 天前
技术文档合著:DeepSeek辅助多人协作文档的风格统一与内容补全
android·大数据·人工智能·微服务·golang·自动化·deepseek
赛恩斯1 天前
安卓构建工具D8和R8的区别
android
—Qeyser1 天前
Flutter CustomScrollView 自定义滚动视图 - 完全指南
android·flutter·ios
鸣弦artha1 天前
Flutter 框架跨平台鸿蒙开发 —— Image Widget 图片处理:圆角、裁剪、阴影
android·flutter·harmonyos
—Qeyser1 天前
Flutter ListView 列表组件完全指南
android·flutter·ios
独自破碎E1 天前
包含min函数的栈
android·java·开发语言·leetcode
毕设源码-邱学长1 天前
【开题答辩全过程】以 基于Android的健康码系统架构为例,包含答辩的问题和答案
android·系统架构
冬奇Lab1 天前
稳定性性能系列之十五——系统稳定性监控体系建设:从指标到预警的完整方案
android·性能优化·debug
沈千秋.1 天前
简单文件包含案例
android·ide·android studio·文件包含
冬奇Lab1 天前
【Kotlin系列06】面向对象进阶:从接口到多态,设计灵活可扩展的代码
android·kotlin·编程语言