Android RecyclerView末尾两项(Item49、Item50)无法显示问题求助
RecyclerView在BottomSheet中无法显示最后两项的解决方案
RecyclerView嵌套在BottomSheet组件中,滚动至底部时仅能显示前48个列表项,最后两个「Item 49」「Item 50」始终无法显示。
问题根源
- 多余的滚动Behavior干扰:RecyclerView上添加了
app:layout_behavior="@string/appbar_scrolling_view_behavior",这个Behavior是专门配合AppBarLayout实现滚动联动的,当前布局中没有AppBar组件,会导致RecyclerView的布局计算异常,底部内容被截断。 - BottomSheet容器高度设置不合理:承载BottomSheet的LinearLayout使用了
android:layout_height="match_parent",结合behavior_peekHeight="462dp"的固定折叠高度,会让RecyclerView的底部区域被BottomSheet的布局逻辑遮挡。
修复方案
方案一:移除多余Behavior并调整容器高度
修改布局文件中的两处关键设置:
- 删除RecyclerView的
app:layout_behavior="@string/appbar_scrolling_view_behavior"属性 - 将BottomSheet对应的LinearLayout的
android:layout_height从match_parent改为wrap_content
修改后的完整布局代码:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools"> <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"> <LinearLayout android:id="@+id/sheetBehaviour" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" app:behavior_hideable="false" app:behavior_peekHeight="462dp"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycleview" android:layout_width="match_parent" android:layout_height="match_parent" tools:itemCount="50" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" tools:listitem="@layout/layout_items" android:nestedScrollingEnabled="false" /> </LinearLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.constraintlayout.widget.ConstraintLayout>
方案二:保留容器高度,添加RecyclerView底部内边距
如果需要维持LinearLayout的match_parent高度,可以给RecyclerView添加足够的底部内边距,确保最后两项不被遮挡:
<androidx.recyclerview.widget.RecyclerView android:id="@+id/recycleview" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="80dp" <!-- 数值可根据Item高度调整 --> tools:itemCount="50" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" tools:listitem="@layout/layout_items" android:nestedScrollingEnabled="false" />
效果验证
修改完成后重新运行应用,RecyclerView即可正常滚动显示全部50个列表项。
内容的提问来源于stack exchange,提问作者Aditya Sharma
相关产品推荐
相关产品推荐

