美文网首页
最简单的ReclyclerView增加分割线

最简单的ReclyclerView增加分割线

作者: kevinsEegets | 来源:发表于2020-05-12 11:12 被阅读0次

    实现代码

    
    import android.content.res.Resources
    import android.graphics.Rect
    import android.util.TypedValue
    import android.view.View
    import androidx.recyclerview.widget.RecyclerView
    import androidx.recyclerview.widget.RecyclerView.ItemDecoration
    
    /**
     * @data on  上午10:50
     * @author Kevin
     * @describe RecyclerView 分割线
     * @param space item之间间距
     * @param orientation 排列方向 垂直(VERTICAL) 水平(HORIZONTAL), 默认为垂直
     * @param startSpace 起始间距(第一个item距离屏幕左侧间距)
     * @param endSpace 终止间距(最后一个item离屏幕后侧间距)
     **/
    
    class DefItemDecoration constructor(val space: Int = 0, private val orientation: DefOrientation? = DefOrientation.VERTICAL, private val startSpace: Int = 0, private val endSpace: Int = 0) : ItemDecoration() {
    
        override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
            val position = parent.getChildAdapterPosition(view)
            val totalCount = parent.adapter?.itemCount
            val defSpace = dip2px(space.toFloat())
            val defStartSpace = dip2px(startSpace.toFloat())
            val defEndSpace = dip2px(endSpace.toFloat())
            if (totalCount == 1) {
                if (orientation == DefOrientation.VERTICAL) {
                    outRect.top = defStartSpace
                    outRect.bottom = defEndSpace
                } else {
                    outRect.left = defStartSpace
                    outRect.right = defEndSpace
                }
                return
            }
            if (orientation == DefOrientation.VERTICAL) {
                when (position) {
                    0 -> { //第一个
                        outRect.top = defStartSpace
                    }
                    totalCount?.minus(1) -> { //最后一个
                        if (totalCount == 2) {
                            outRect.top = defSpace / 2
                        }
                        outRect.bottom = defEndSpace
                    }
                    else -> { //中间其它的
                        outRect.top = defSpace / 2
                        outRect.bottom = defSpace / 2
                    }
                }
            } else {
                when (position) {
                    0 -> { //第一个
                        outRect.left = defStartSpace
                    }
                    totalCount?.minus(1) -> { //最后一个
                        if (totalCount == 2) {
                            outRect.left = defSpace / 2
                        }
                        outRect.right = defEndSpace
                    }
                    else -> { //中间其它的
                        outRect.left = defSpace / 2
                        outRect.right = defSpace / 2
                    }
                }
            }
        }
    
    
        fun dip2px(dipValue: Float): Int {
            return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, Resources.getSystem().displayMetrics).toInt()
        }
    
        enum class DefOrientation {
            VERTICAL, HORIZONTAL
        }
    }
    

    调用方式

     collocationRecyclerView?.apply {
                this.addItemDecoration(DefItemDecoration(30, DefItemDecoration.DefOrientation.HORIZONTAL))
            }
    

    相关文章

      网友评论

          本文标题:最简单的ReclyclerView增加分割线

          本文链接:https://www.haomeiwen.com/subject/amzvnhtx.html