RecyclerView萬能分割線_線性分割線_網格分割線

DividerDecoration

簡述:

RecyclerView分割線,
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

功能:

  1. 支持設置分割線寬度,左右上下的margin
  2. 支持繪製分割線的長度是依據整體recyclerView還是依據item
  3. 支持設置自定義繪製分割線
  4. 支持設置不同位置的分割線的顯示隱藏
  5. 支持線性分割線
  6. 支持網格分割線

使用:

一,線性分割線

    recyclerView.layoutManager = LinearLayoutManager(this, OrientationHelper.VERTICAL, false)
    recyclerView.addItemDecoration(
        LinerItemDecoration.Builder(this, OrientationHelper.VERTICAL)
            .setDividerWidthPx(20)//分割線的寬度 單位px
            .setDividerMarginPx(10, 10, 10, 20)//設置分割線距離item的間隔
            .setDividerDrawByChild(true)//設置繪製分割線的長度是否是根據item的長度來繪製 默認爲false代表繪製是根據RecyclerView的長度來的
            .setHeadViewCount(0)//設置頭佈局的個數 默認爲0 頭佈局之間沒有分割線 以及頭佈局與第一條數據之間也是沒有分割線
            .setFooterViewCount(0)//設置尾佈局的個數 默認爲0 尾佈局之間沒有分割線
            .showLastDivider(false)//最後一個item後面是否有分割線 默認爲false
            .setDividerColorProvider(object : BaseItemDecoration.DividerColorProvider {
                override fun getDividerColor(position: Int, parent: RecyclerView): Int {
                    when ((position + 1) % 4) {
                        0 -> {
                            return Color.parseColor("#FF0000")
                        }
                        1 -> {
                            return Color.parseColor("#00FF00")
                        }
                        2 -> {
                            return Color.parseColor("#0000FF")
                        }
                        3 -> {
                            return Color.parseColor("#000000")
                        }
                        else -> {
                            return Color.parseColor("#000000")
                        }
                    }

                }
            })//設置分割線繪製的顏色  我們可以設置在不同的位置繪製不同的顏色
            .setDividerVisibleProvider(object : BaseItemDecoration.DividerVisibleProvider {
                override fun shouldHideDivider(position: Int, parent: RecyclerView): Boolean {
                    //在3的倍數位置 不顯示顏色
                    return (position + 1) % 3 == 0
                }
            })//設置在某個位置隱藏分割線 但是分割線的間隔還是在的,只是不再繪製而已
            .build()
    )

二、網格分割線

    recyclerView.layoutManager = GridLayoutManager(this, 3, OrientationHelper.VERTICAL, false)
    recyclerView.addItemDecoration(
        GridItemDecoration.Builder(this, OrientationHelper.VERTICAL)
            .setDividerWidthPx(10)
            .setFooterViewCount(0)
            .setHeadViewCount(0)
            .setDividerMarginPx(10, 10, 10, 10)
            .setDividerColorProvider(object : BaseItemDecoration.DividerColorProvider {
                override fun getDividerColor(position: Int, parent: RecyclerView): Int {
                    return Color.parseColor("#FF0000")
                }

            })
            .build()
    )

項目地址!!!

:點擊我,帶走我 _

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章