前言
网上设置RecycleView间隔的几种方法总结
- item布局中,设置间隔
- 自定义ItemDecoration 实现间隔,又分为两种:
2.1 使用Rect 设置left、top 、right 、bottom 设置间距,伪造间隔
2.2 使用draw 绘制间隔(onDraw、onDrawOver) - 官方默认的分割线
三种方法,无优劣之分,各有适用的场景。
方法对比
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&#