有的时候我将 RecyclerView 嵌套 在 ScrollView 里面, 会发现 RecyclerView 内容多了会有滚动条,这时候我们理想 是 RecyclerView 可以自动高度,不要出现滚动条。
这时候,我们需要解决 RecyclerView 自动高度问题 和 RecyclerView 不要出现滚动条
注意: 由于API版本不同,可能效果不一样,以下针对API 24以上(其他版本可以尝试)
为了解决 RecyclerView 的 layout_height 设置成 wrap_content 失效,并不是按照实际内容展示高度,解决这个问题,需要在外面套一层 RelativeLayout, 将属性 descendantFocusability 设置成 blocksDescendants
<!-- 防止出现 RecyclerView 会出现滚动条,显示不全 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
将 RecyclerView 设置成不需要滚动条, 将 setNestedScrollingEnabled 设置成 false
recyclerView.setNestedScrollingEnabled(false);