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>
相关推荐
白云LDC5 小时前
Android Studio新建Vecter asset一直显示Loading icons(转圈圈)的解决办法
android·ide·android studio
Rytter8 小时前
某气骑士 libtprt.so 反 Frida 机制分析与绕过
android·安全·网络安全
alexhilton9 小时前
揭密:Compose应用如何做到启动提升34%
android·kotlin·android jetpack
沐言人生11 小时前
React Native 源码分析1——HybridData 机制深度分析
android·react native
程序员陆业聪11 小时前
跨平台框架全景图:Flutter/KMP/KuiKly/RN的2026年格局
android
码云数智-园园12 小时前
Fibers(纤程)来了:打破阻塞,实现纯PHP下的异步非阻塞IO
android
shaoming377615 小时前
检查系统硬件配置是否满足PyCharm最低要求
android·spring boot·mysql
一起搞IT吧16 小时前
高通Camx功能feature分析之十五:insensor zoom介绍及实现
android·智能手机·相机
aqi0017 小时前
一文读懂 HarmonyOS 6.1 带来的十大重要升级
android·华为·harmonyos·鸿蒙·harmony
秋919 小时前
MySQL 9.7.0 使用详解:新特性、实战与避坑指南
android·数据库·mysql