如何给RecycleView 设置间隔?

本文总结了RecycleView设置间隔的三种方法:在item布局中设置、自定义ItemDecoration通过Rect设置和使用onDraw绘制。分别探讨了每种方法的实现原理、效果以及适用场景,帮助开发者更好地理解和选择合适的间隔设置方式。
摘要由CSDN通过智能技术生成

前言

网上设置RecycleView间隔的几种方法总结

  1. item布局中,设置间隔
  2. 自定义ItemDecoration 实现间隔,又分为两种:
    2.1 使用Rect 设置left、top 、right 、bottom 设置间距,伪造间隔
    2.2 使用draw 绘制间隔(onDraw、onDrawOver)
  3. 官方默认的分割线
    三种方法,无优劣之分,各有适用的场景。

方法对比

1.item布局设置分割线
1.1 顶部带分割线(LinearLayoutManager.VERTICAL)
效果

在这里插入图片描述

<?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="wrap_content"
    android:orientation="vertical">
    <!--分割线-->
    <View
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:background="#FFFFFF" />

    <ImageView
        android:id="@+id/img_item"![在这里插入图片描述](https://img-blog.csdnimg.cn/20190712210659517.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTMwNDA4MTk=,size_16,color_FFFFFF,t_70)
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@color/colorPrimary" />

</LinearLayout>

1.2 顶部无间距(LinearLayoutManager.VERTICAL)
效果


在这里插入图片描述

<?xml version="1.0" encoding="utf-8&#
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值