Android RecycleView 虛線實現(xiàn)代碼

DividerItemDecoration類
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.annotation.ColorInt
import androidx.recyclerview.widget.RecyclerView

abstract class DividerItemDecoration : RecyclerView.ItemDecoration() {
    private val mPaint by lazy { Paint(Paint.ANTI_ALIAS_FLAG) }
    abstract fun getDivider(itemPosition: Int): Divider

    init {
        mPaint.style = Paint.Style.FILL
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        //left, top, right, bottom
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val itemPosition = (child.layoutParams as RecyclerView.LayoutParams).viewLayoutPosition
            val divider = getDivider(itemPosition)
            divider.leftSideLine?.let {
                val leftLineWidthPx = it.widthPx
                if (leftLineWidthPx > 0) {
                    val startPaddingPx = it.startPaddingPx
                    val endPaddingPx = it.endPaddingPx
                    drawChildLeftVertical(child, c, it.color, leftLineWidthPx, startPaddingPx, endPaddingPx)
                }
            }
            divider.topSideLine?.let {
                val topLineWidthPx = it.widthPx
                if (topLineWidthPx > 0) {
                    val startPaddingPx = it.startPaddingPx
                    val endPaddingPx = it.endPaddingPx
                    drawChildTopHorizontal(child, c, it.color, topLineWidthPx, startPaddingPx, endPaddingPx)
                }
            }
            divider.rightSideLine?.let {
                val rightLineWidthPx = it.widthPx
                if (rightLineWidthPx > 0) {
                    val startPaddingPx = it.startPaddingPx
                    val endPaddingPx = it.endPaddingPx
                    drawChildRightVertical(child, c, it.color, rightLineWidthPx, startPaddingPx, endPaddingPx)
                }
            }
            divider.bottomSideLine?.let {
                val bottomLineWidthPx = it.widthPx
                if (bottomLineWidthPx > 0) {
                    val startPaddingPx = it.startPaddingPx
                    val endPaddingPx = it.endPaddingPx
                    drawChildBottomHorizontal(child, c, it.color, bottomLineWidthPx, startPaddingPx, endPaddingPx)
                }
            }
        }
    }

    private fun drawChildBottomHorizontal(child: View, c: Canvas, @ColorInt color: Int, lineWidthPx: Int, startPaddingPx: Int, endPaddingPx: Int) {
        val leftPadding = if (startPaddingPx <= 0) {
            //padding<0當(dāng)作==0處理
            //上下左右默認(rèn)分割線的兩頭都出頭一個分割線的寬度,避免十字交叉的時候卑雁,交叉點是空白
            -lineWidthPx
        } else {
            startPaddingPx
        }
        val rightPadding = if (endPaddingPx > 0) {
            -endPaddingPx
        } else {
            lineWidthPx
        }
        val params = child
                .layoutParams as RecyclerView.LayoutParams
        val left = child.left - params.leftMargin + leftPadding
        val right = child.right + params.rightMargin + rightPadding
        val top = child.bottom + params.bottomMargin
        val bottom = top + lineWidthPx
        mPaint.color = color

        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mPaint)

    }

    private fun drawChildTopHorizontal(child: View, c: Canvas, @ColorInt color: Int, lineWidthPx: Int, startPaddingPx: Int, endPaddingPx: Int) {
        val leftPadding = if (startPaddingPx <= 0) {
            -lineWidthPx
        } else {
            startPaddingPx
        }
        val rightPadding = if (endPaddingPx <= 0) {
            lineWidthPx
        } else {
            -endPaddingPx
        }
        val params = child
                .layoutParams as RecyclerView.LayoutParams
        val left = child.left - params.leftMargin + leftPadding
        val right = child.right + params.rightMargin + rightPadding
        val bottom = child.top - params.topMargin
        val top = bottom - lineWidthPx
        mPaint.color = color
        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mPaint)

    }

    private fun drawChildLeftVertical(child: View, c: Canvas, @ColorInt color: Int, lineWidthPx: Int, startPaddingPx: Int, endPaddingPx: Int) {
        val topPadding = if (startPaddingPx <= 0) {
            -lineWidthPx
        } else {
            startPaddingPx
        }
        val bottomPadding = if (endPaddingPx <= 0) {
            lineWidthPx
        } else {
            -endPaddingPx
        }
        val params = child
                .layoutParams as RecyclerView.LayoutParams
        val top = child.top - params.topMargin + topPadding
        val bottom = child.bottom + params.bottomMargin + bottomPadding
        val right = child.left - params.leftMargin
        val left = right - lineWidthPx
        mPaint.color = color
        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mPaint)
    }

    private fun drawChildRightVertical(child: View, c: Canvas, @ColorInt color: Int, lineWidthPx: Int, startPaddingPx: Int, endPaddingPx: Int) {
        val topPadding = if (startPaddingPx <= 0) {
            -lineWidthPx
        } else {
            startPaddingPx
        }
        val bottomPadding = if (endPaddingPx <= 0) {
            lineWidthPx
        } else {
            -endPaddingPx
        }
        val params = child
                .layoutParams as RecyclerView.LayoutParams
        val top = child.top - params.topMargin + topPadding
        val bottom = child.bottom + params.bottomMargin + bottomPadding
        val left = child.right + params.rightMargin
        val right = left + lineWidthPx
        mPaint.color = color
        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mPaint)
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        val itemPosition = (view.layoutParams as RecyclerView.LayoutParams).viewLayoutPosition
        val divider = getDivider(itemPosition)
        val left = divider.leftSideLine?.widthPx ?: 0
        val top = divider.topSideLine?.widthPx ?: 0
        val right = divider.rightSideLine?.widthPx ?: 0
        val bottom = divider.bottomSideLine?.widthPx ?: 0
        outRect.set(left, top, right, bottom)
    }
}
Divider類
class Divider(var leftSideLine: SideLine?, var topSideLine: SideLine?, var rightSideLine: SideLine?, var bottomSideLine: SideLine?) {
    class Builder {
        var leftSideLine: SideLine? = null
        var topSideLine: SideLine? = null
        var rightSideLine: SideLine? = null
        var bottomSideLine: SideLine? = null

        fun setLeftSideLine(leftSideLine: SideLine): Builder {
            this.leftSideLine = leftSideLine
            return this
        }

        fun setTopSideLine(topSideLine: SideLine): Builder {
            this.topSideLine = topSideLine
            return this
        }

        fun setRightSideLine(rightSideLine: SideLine): Builder {
            this.rightSideLine = rightSideLine
            return this
        }

        fun setBottomSideLine(bottomSideLine: SideLine): Builder {
            this.bottomSideLine = bottomSideLine
            return this
        }

        fun builder(): Divider {
            return Divider(leftSideLine, topSideLine, rightSideLine, bottomSideLine)
        }
    }
}
SideLine類
class SideLine constructor(@ColorInt var color: Int, var widthPx: Int, var startPaddingPx: Int = 0, var endPaddingPx: Int = 0) {
    constructor(@ColorInt color: Int, widthPx: Int) : this(color, widthPx, 0, 0)
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市瑰艘,隨后出現(xiàn)的幾起案子捶障,更是在濱河造成了極大的恐慌僧须,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,723評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件项炼,死亡現(xiàn)場離奇詭異担平,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)锭部,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,485評論 2 382
  • 文/潘曉璐 我一進(jìn)店門暂论,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人拌禾,你說我怎么就攤上這事取胎。” “怎么了湃窍?”我有些...
    開封第一講書人閱讀 152,998評論 0 344
  • 文/不壞的土叔 我叫張陵闻蛀,是天一觀的道長。 經(jīng)常有香客問我您市,道長觉痛,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,323評論 1 279
  • 正文 為了忘掉前任茵休,我火速辦了婚禮薪棒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘榕莺。我一直安慰自己俐芯,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 64,355評論 5 374
  • 文/花漫 我一把揭開白布帽撑。 她就那樣靜靜地躺著泼各,像睡著了一般鞍时。 火紅的嫁衣襯著肌膚如雪亏拉。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,079評論 1 285
  • 那天逆巍,我揣著相機(jī)與錄音及塘,去河邊找鬼。 笑死锐极,一個胖子當(dāng)著我的面吹牛笙僚,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播灵再,決...
    沈念sama閱讀 38,389評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼肋层,長吁一口氣:“原來是場噩夢啊……” “哼亿笤!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起栋猖,我...
    開封第一講書人閱讀 37,019評論 0 259
  • 序言:老撾萬榮一對情侶失蹤净薛,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后蒲拉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體肃拜,經(jīng)...
    沈念sama閱讀 43,519評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,971評論 2 325
  • 正文 我和宋清朗相戀三年雌团,在試婚紗的時候發(fā)現(xiàn)自己被綠了燃领。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,100評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡锦援,死狀恐怖猛蔽,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情灵寺,我是刑警寧澤枢舶,帶...
    沈念sama閱讀 33,738評論 4 324
  • 正文 年R本政府宣布,位于F島的核電站替久,受9級特大地震影響凉泄,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蚯根,卻給世界環(huán)境...
    茶點故事閱讀 39,293評論 3 307
  • 文/蒙蒙 一后众、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧颅拦,春花似錦蒂誉、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,289評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至碌秸,卻和暖如春绍移,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背讥电。 一陣腳步聲響...
    開封第一講書人閱讀 31,517評論 1 262
  • 我被黑心中介騙來泰國打工蹂窖, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人恩敌。 一個月前我還...
    沈念sama閱讀 45,547評論 2 354
  • 正文 我出身青樓瞬测,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子月趟,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,834評論 2 345

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