Android Recyclerview设置Item之间的间距

其实不需要自定义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的底部内边距。


版权声明:本文为jb_home原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。