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


    前言:使用NestedScrollView嵌套RecyclerView。

    解决步骤一:固定高度

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

    1.      binding.nestedScrollV.post(new Runnable() {
    2.         @Override
    3.         public void run() {
    4. binding.selectList.getLayoutParams().height = binding.nestedScrollV.getHeight(); // 使用NestedScrollView的高度
    5. binding.productList.getLayoutParams().height = binding.nestedScrollV.getHeight();
    6. binding.selectList.setLayoutParams(binding.selectList.getLayoutParams());
    7. binding.productList.setLayoutParams(binding.productList.getLayoutParams());
    8.      });

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

    1. public class MNestedScrollViewBox extends NestedScrollView {
    2. public MNestedScrollViewBox(@NonNull Context context, @Nullable AttributeSet attrs) {
    3. super(context, attrs);
    4. }
    5. // 使用NestedScrollView嵌套RecyclerView,会导致RecyclerView复用机制失效,RecyclerView会将所有数据一次性全部加载。
    6. // 解决方法:重写measureChildWithMargins,让NestedScrollView测量RecyclerView时 不使用MeasureSpec.UNSPECIFIED模式即可。
    7. @Override
    8. protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
    9. child.measure(parentWidthMeasureSpec, parentHeightMeasureSpec);
    10. }
    11. }

    使用:

    1. "1.0" encoding="utf-8"?>
    2. "http://schemas.android.com/apk/res/android"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent"
    6. android:background="@color/color_F5F6F7"
    7. android:fillViewport="true"
    8. android:overScrollMode="never"
    9. tools:context=".ui.fragment.shopdetail.OrderFoodFragment">
    10. android:layout_width="match_parent"
    11. android:layout_height="match_parent">
    12. android:id="@+id/select_list"
    13. android:layout_width="84dp"
    14. android:layout_height="match_parent"
    15. android:overScrollMode="never"
    16. tools:listitem="@layout/widget_select_item_ho" />
    17. android:id="@+id/product_list"
    18. android:layout_width="0dp"
    19. android:layout_height="match_parent"
    20. android:layout_weight="1"
    21. android:background="@color/white"
    22. android:overScrollMode="never"
    23. android:paddingStart="8dp"
    24. tools:ignore="RtlSymmetry"
    25. tools:listitem="@layout/widget_product_item_ho" />

  • 相关阅读:
    adb 获取 Android 设备中已安装的 apk 文件
    2023山东老博会·CSOLDE中国国际养老服务业展览会
    LVS负载均衡群集
    倍福TwinCAT3 Ads相关错误详细列表
    B+树索引(7)之索引适用场景(上)
    【毕业设计】基于SSM的OA办公管理系统的设计与实现 -java web
    【1107】
    C#语法基础
    springboot 自动装配原理
    有什么可替代问卷星的好用的问卷工具?
  • 原文地址:https://blog.csdn.net/Lan_Se_Tian_Ma/article/details/134510946