需求分析
如图所示的:
101(共2人)
这一行, 顶部有 一块白色区域, 可以当做是分割线101(共2人)
这一行, 底部有一个很细的分割线, 差不多撑满了一行人物信息
这一行, 首次出现时, 只有底部有分割线, 而且还是 非撑满一行的效果人物信息
这一行, 最后出现时, 底部没有分割线
不出意外, 很多同学的实现方式, 都是在 布局的xml
中, 用padding
maring
backgroud
实现.
但是遇到这种相同type类型的item, 只有中间有分割线, 最后一条没有分割线
的情况, 估计就有点棘手了.
莫慌, 看完本文, 你能轻松应对各种恶心的分割线.
继承 RecyclerView.ItemDecoration
, 实现 核心的 getItemOffsets
onDraw
onDrawOver
方法, 通常 只需要实现 getItemOffsets
onDraw
即可. 如果需要悬停效果, 才需要实现 onDrawOver
为了让封装更轻量
, 所以, 我并打算有任何实际操作方法.
只做核心业务的封装, 具体实现 丢给调用者.
override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
eachChildViewHolder(parent) { beforeViewHolder, viewHolder, afterViewHolder ->
eachItemDoIt.invoke(canvas, parent, state, null, beforeViewHolder, viewHolder, afterViewHolder, false)
}
}
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
eachChildViewHolder(parent, view) { beforeViewHolder, viewHolder, afterViewHolder ->
eachItemDoIt.invoke(null, parent, state, outRect, beforeViewHolder, viewHolder, afterViewHolder, false)
}
}
核心方法
fun eachChildViewHolder(
parent: RecyclerView,
targetView: View? = null,/*指定目标, 则只回调目标前后的ViewHolder*/
callback: (
beforeViewHolder: RecyclerView.ViewHolder?,
viewHolder: RecyclerView.ViewHolder,
afterViewHolder: RecyclerView.ViewHolder?
) -> Unit
) {
val childCount = parent.childCount
for (i in 0 until childCount) {
val child = parent.getChildAt(i)
val childViewHolder = parent.findContainingViewHolder(child)
childViewHolder?.let {
//前一个child
var beforeViewHolder: RecyclerView.ViewHolder? = null
//后一个child
var afterViewHolder: RecyclerView.ViewHolder? = null
if (i >= 1) {
beforeViewHolder = parent.findContainingViewHolder(parent.getChildAt(i - 1))
}
if (i < childCount - 1) {
afterViewHolder = parent.findContainingViewHolder(parent.getChildAt(i + 1))
}
if (targetView != null) {
if (targetView == child) {
callback.invoke(beforeViewHolder, it as RBaseViewHolder, afterViewHolder)
return
}
} else {
callback.invoke(beforeViewHolder, it as RBaseViewHolder, afterViewHolder)
}
}
}
}
通过此方法, 到RecyclerView
触发onDraw
时, 枚举界面上的所有childView
, 并且计算拿到 前一个
和 后一个
childView
的viewHolder
对象.(如果有)
这样就可以通过 ViewHolder.getItemViewType
来区分, 各个item
的类型, 也能够通过,
与前一个
比较, 判断是否是同类型的第一个位置
与后一个
比较, 判断是否是同类型的最后一个位置
有个你想要的各种信息, 绘制分割线的事, 就交给 canvas
吧.
完整代码
class DslItemDecoration : RecyclerView.ItemDecoration() {
val paint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
val tempDrawRect = Rect()
/**
* 将3个方法, 合一调用. 通过参数, 来区分是那一个方法.
*
* outRect 不为空时, 是 getItemOffsets 方法
* canvas 不为空时, 是 onDrawOver onDraw
* isOverDraw 控制是否是 onDrawOver
* */
var eachItemDoIt: (
canvas: Canvas?, parent: RecyclerView, state: RecyclerView.State, outRect: Rect?,
beforeViewHolder: RecyclerView.ViewHolder?,
viewHolder: RecyclerView.ViewHolder,
afterViewHolder: RecyclerView.ViewHolder?,
isOverDraw: Boolean
) -> Unit =
{ _, _, _, _, _, _, _, _ -> }
override fun onDrawOver(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
eachChildRViewHolder(parent) { beforeViewHolder, viewHolder, afterViewHolder ->
eachItemDoIt.invoke(canvas, parent, state, null, beforeViewHolder, viewHolder, afterViewHolder, true)
}
}
override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
eachChildRViewHolder(parent) { beforeViewHolder, viewHolder, afterViewHolder ->
eachItemDoIt.invoke(canvas, parent, state, null, beforeViewHolder, viewHolder, afterViewHolder, false)
}
}
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
eachChildRViewHolder(parent, view) { beforeViewHolder, viewHolder, afterViewHolder ->
eachItemDoIt.invoke(null, parent, state, outRect, beforeViewHolder, viewHolder, afterViewHolder, false)
}
}
fun eachChildViewHolder(
parent: RecyclerView,
targetView: View? = null,/*指定目标, 则只回调目标前后的ViewHolder*/
callback: (
beforeViewHolder: RecyclerView.ViewHolder?,
viewHolder: RecyclerView.ViewHolder,
afterViewHolder: RecyclerView.ViewHolder?
) -> Unit
) {
val childCount = parent.childCount
for (i in 0 until childCount) {
val child = parent.getChildAt(i)
val childViewHolder = parent.findContainingViewHolder(child)
childViewHolder?.let {
//前一个child
var beforeViewHolder: RecyclerView.ViewHolder? = null
//后一个child
var afterViewHolder: RecyclerView.ViewHolder? = null
if (i >= 1) {
beforeViewHolder = parent.findContainingViewHolder(parent.getChildAt(i - 1))
}
if (i < childCount - 1) {
afterViewHolder = parent.findContainingViewHolder(parent.getChildAt(i + 1))
}
if (targetView != null) {
if (targetView == child) {
callback.invoke(beforeViewHolder, it as RBaseViewHolder, afterViewHolder)
return
}
} else {
callback.invoke(beforeViewHolder, it as RBaseViewHolder, afterViewHolder)
}
}
}
}
}
调用例子
public fun RecyclerView.dslItemDecoration(init: DslItemDecoration.() -> Unit) {
addItemDecoration(DslItemDecoration().apply {
init()
})
}
dslItemDecoration {
val line = getDimen(R.dimen.wt_line)
eachItemDoIt = { canvas, _, _, outRect, _, viewHolder, afterViewHolder, _ ->
if (viewHolder.itemViewType == R.layout.item_info_group_head) {
outRect?.set(0, 10 * dpi, 0, line)
tempDrawRect.set(
0,
viewHolder.itemView.top - 10 * dpi,
viewHolder.itemView.right,
viewHolder.itemView.top
)
paint.color = getColor(R.color.wt_dark_bg)
canvas?.drawRect(tempDrawRect, paint)
tempDrawRect.set(
0,
viewHolder.itemView.top - line,
viewHolder.itemView.right,
viewHolder.itemView.top
)
paint.color = getColor(R.color.wt_line)
canvas?.drawRect(tempDrawRect, paint)
tempDrawRect.set(
0,
viewHolder.itemView.bottom,
viewHolder.itemView.right,
viewHolder.itemView.bottom - line
)
canvas?.drawRect(tempDrawRect, paint)
} else if (viewHolder.itemViewType == R.layout.item_info_single_text) {
if (afterViewHolder?.itemViewType == R.layout.item_info_single_text) {
outRect?.set(0, 0, 0, line)
paint.color = getColor(R.color.wt_line)
tempDrawRect.set(
25 * dpi,
viewHolder.itemView.bottom,
viewHolder.itemView.right,
viewHolder.itemView.bottom - line
)
canvas?.drawRect(tempDrawRect, paint)
}
}
}
}
结束…
群内有各(pian)种(ni)各(jin)样(qun)
的大佬,等你来撩.
点此快速加群
请使用QQ扫码加群, 小伙伴们都在等着你哦!
关注我的公众号, 每天都能一起玩耍哦!