官方提供的分割線處理方案,也是繼承的RecyclerView.ItemDecoration
實現(xiàn)的。
divider.png
用法很簡單,注釋里面有demo。同時也可以通過自定義drawable來實現(xiàn)divider的自定義。
1.用系統(tǒng)提供的高度和顏色,不做自定義。
DividerItemDecoration dec = new DividerItemDecoration(mContext,DividerItemDecoration.VERTICAL);
2.DividerItemDecoration 可以通過setDrawable(Drawable drawable)
來設(shè)置具體的分割線內(nèi)容
ColorDrawable:
itemDecoration.setDrawable(new ColorDrawable(ContextCompat.getColor(mContext,R.color.bg_ddddde)));
xml中自定義shape:
自定義shape可以設(shè)置分割線的高度和顏色。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:height="10dp"/>
<solid android:color="@color/bg_ddddde"/>
</shape>
Drawable drawable = ContextCompat.getDrawable(mContext,R.drawable.shape_question_diveder);
使用drawable中的圖片:
使用圖片的話,分割線的高度就是圖片的高度,圖片會有拉伸。
Drawable drawable = ContextCompat.getDrawable(mContext,R.mipmap.ic_launcher);
最后通過DividerItemDecoration.setDrawable(Drawable drawable)
就能看到效果了,省了不少力氣。