RecyclerView設(shè)置分割線---DividerItemDecoration

官方提供的分割線處理方案,也是繼承的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)就能看到效果了,省了不少力氣。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容