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>
相关推荐
半桔24 分钟前
【Linux手册】从接口到管理:Linux文件系统的核心操作指南
android·java·linux·开发语言·面试·系统架构
xiaopengbc43 分钟前
Android解压工具,ZArchiver,RAR for Android,iZip,The Unarchiver,解压专家
android
Digitally43 分钟前
5种使用USB数据线将文件从安卓设备传输到电脑的方法
android
爬虫程序猿9 小时前
利用爬虫按关键字搜索淘宝商品实战指南
android·爬虫
顾北川_野9 小时前
Android ttyS2无法打开该如何配置 + ttyS0和ttyS1可以
android·fpga开发
wzj_what_why_how12 小时前
Android网络层架构:统一错误处理的问题分析到解决方案与设计实现
android·架构
千里马学框架12 小时前
User手机上如何抓取界面的布局uiautomatorviewer
android·智能手机·aosp·uiautomator·布局抓取·user版本
阿巴~阿巴~13 小时前
操作系统核心技术剖析:从Android驱动模型到鸿蒙微内核的国产化实践
android·华为·harmonyos
hsx66614 小时前
使用 MaterialShapeDrawable 自定义各种形状的 View
android
用户20187928316714 小时前
滑动城堡的奇妙管家 ——ViewPager故事
android