背景:页面布局,最外层有个ScrollView,然后里面有个RecyclerView,然后每个RecyclerView的item都是一个RecyclerView
异常:页面展示不完整,最底下的Item 展示一半,在往上滑就滑不动了

解决:
// 每一个item渲染完后重新计算外层recyclerview高度 // 因为外层的recyclerview是先渲染的,渲染时 内部recyclerview无数据此时高度是0 // 当内部recyclerview 渲染完后要重新计算高度 外部recyclerview才可以撑开 //第一步:测量内部recyclerView高度 recommendedRvViewHolder.recyclerView.measure(0, 0); //第二步:重新设置外层的recyclerview高度 已经有的高度 + 新渲染Item的高度 ViewGroup.LayoutParams params = recyclerView.getLayoutParams(); //这里的30 是布局文件中"为你推荐"、"热门榜单"所在布局的高度 18 + 两个item之间的间距 9 27 取整 30 params.height += recommendedRvViewHolder.recyclerView.getMeasuredHeight() + DensityUtil.dip2px(mContext, 30); recyclerView.setLayoutParams(params);
看下图:最底下的item展示出来了

部分核心的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/common_white"
android:orientation="vertical">
<include layout="@layout/search_layout"></include>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.youth.banner.Banner
android:id="@+id/star_film_home_banner"
android:layout_width="match_parent"
android:layout_

本文介绍了一种解决嵌套使用RecyclerView导致页面展示不完整的方法。通过在每个item渲染完毕后,重新计算外层RecyclerView的高度,确保所有内容都能正确显示。此方案适用于页面布局中包含ScrollView和多个嵌套RecyclerView的情况。

3177

被折叠的 条评论
为什么被折叠?



