对于我的自定义装饰器,我想在列表末尾添加一些额外的填充。对于这个用例,我需要知道项目是否在网格的最后一行。
这有可能吗?
到目前为止,装饰代码
class SetupHeaderDecorator(private val paddingHorizontal: Int, private val paddingBeforeHeader: Int, private val paddingTopBottom: Int) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State?) {
val pos = parent.getChildAdapterPosition(view)
val count = parent.adapter.itemCount
var firstRow = pos == 0
val lastRow = pos == count - 1
var leftColumn = true
var rightColumn = true
if (parent.layoutManager is GridLayoutManager) {
// TODO - this calculation still needs to be done!
// firstRow = ...
// lastRow = ...
val lm = parent.layoutManager as GridLayoutManager
val span = lm.spanCount
val col = lm.spanSizeLookup.getSpanIndex(pos, span)
val colEnd = col + lm.spanSizeLookup.getSpanSize(pos) - 1
leftColumn = col == 0
rightColumn = colEnd == (span - 1)
}
// Space above each header, if the header is not the first item
if (pos > 0 && parent.getChildViewHolder(view) is SetupAdapter.ISetupHeaderViewHolder) {
outRect.top = paddingBeforeHeader
}
// First and last row padding top
if (firstRow && paddingTopBottom != 0) {
outRect.top = paddingTopBottom
}
if (lastRow && paddingTopBottom != 0) {
outRect.bottom = paddingTopBottom
}
// row padding left/right
if (paddingHorizontal != 0) {
if (leftColumn)
outRect.left = paddingHorizontal
if (rightColumn)
outRect.right = paddingHorizontal
}
}
}