其实不需要自定义LinearLayout.LayoutParams 跟RecyclerView.ItemDecoration。
假设:要在recycleView的每条item的下加一个8dp的边距:
第一步:
在每个item的布局的根布局加上paddingBottom。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="9dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="13dp"
android:textColor="#ff5e5c5c"
android:textSize="9sp"/>
</RelativeLayout>
</RelativeLayout>
此时前面的所以item都有8dp的下边距,但是最后一条item没有下边距;
第二步:
在RecycleView里设置属性clipToPadding、paddingBottom就可以了
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recy_alert_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="8dp"/>
此处的paddingBottom由于受到clipToPadding属性的影响并不会作用到所有的子item中,设置完以后,你就会看到,最后一条item也有了8dp的底部内边距。
本文介绍了如何在RecyclerView中为每个item添加底部内边距,特别是解决最后一项缺失下边距的问题。通过在item布局文件中设置根布局的paddingBottom,然后在RecyclerView中设置clipToPadding为false并添加paddingBottom,可以确保所有item包括最后一项都有8dp的下边距。这种方法避免了自定义LayoutParams和ItemDecoration的复杂性。
1107

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



