RecyclerView:使用自定义 ItemDecoration 为最后一项添加底部间距不适用于 DividerItemDecoration 和 ItemTouchHelper
RecyclerView: Adding bottom spacing for last item with a custom ItemDecoration doesn't work well with DividerItemDecoration and ItemTouchHelper
我在 RecyclerView 的最后一个项目下面添加一个 space 使用这个流行且有效的解决方案:
class ListMarginDecorator(
private val left: Int = 0,
private val top: Int = 0,
private val right: Int = 0,
private val bottom: Int = 0,
) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
val position = parent.getChildAdapterPosition(view)
outRect.left = left
outRect.top = if (position == 0) top else 0
outRect.right = right
outRect.bottom = if (position == state.itemCount - 1) bottom else 0
}
}
我还使用 DividerItemDecoration, and enable drag-to-reorder by implementing ItemTouchHelper 添加项目分隔符。
下面是我如何在片段 class:
中使用这些 ItemDecorators
binding.recyclerView.addItemDecoration(
DividerItemDecoration(
binding.rvCurrencies.context,
DividerItemDecoration.VERTICAL
)
)
binding.recyclerView.addItemDecoration(
ListMarginDecorator(
bottom = resources.getDimensionPixelSize(R.dimen.list_last_item_bottom_margin) // = 88dp
)
)
我在最后一项下看到 space 这种方法有两个问题。
首先是 ListMarginDecorator 似乎在应用填充,而不是边距,因此列表中最后一项的底部分隔线绘制在 下方 应用的间距最后一项。
第二个问题是我无法再将列表中的项目拖到最底部的位置。
当我注释掉添加 ListMarginDecorator 的行时,这两个都按预期工作:
有没有其他方法可以有效地在最后一项下添加 space,而不会 运行 进入这些问题?
如果你想要 space 在最后一项之后,为什么不直接使用
rvCurrencies.setPadding(0, 0, 0, 100)
rvCurrencies.clipToPadding = false
或在XML
android:paddingBottom="100dp"
android:clipToPadding="false"
这样,你的两个问题就迎刃而解了。
我在 RecyclerView 的最后一个项目下面添加一个 space 使用这个流行且有效的解决方案:
class ListMarginDecorator(
private val left: Int = 0,
private val top: Int = 0,
private val right: Int = 0,
private val bottom: Int = 0,
) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
val position = parent.getChildAdapterPosition(view)
outRect.left = left
outRect.top = if (position == 0) top else 0
outRect.right = right
outRect.bottom = if (position == state.itemCount - 1) bottom else 0
}
}
我还使用 DividerItemDecoration, and enable drag-to-reorder by implementing ItemTouchHelper 添加项目分隔符。
下面是我如何在片段 class:
中使用这些 ItemDecoratorsbinding.recyclerView.addItemDecoration(
DividerItemDecoration(
binding.rvCurrencies.context,
DividerItemDecoration.VERTICAL
)
)
binding.recyclerView.addItemDecoration(
ListMarginDecorator(
bottom = resources.getDimensionPixelSize(R.dimen.list_last_item_bottom_margin) // = 88dp
)
)
我在最后一项下看到 space 这种方法有两个问题。
首先是 ListMarginDecorator 似乎在应用填充,而不是边距,因此列表中最后一项的底部分隔线绘制在 下方 应用的间距最后一项。
第二个问题是我无法再将列表中的项目拖到最底部的位置。
当我注释掉添加 ListMarginDecorator 的行时,这两个都按预期工作:
有没有其他方法可以有效地在最后一项下添加 space,而不会 运行 进入这些问题?
如果你想要 space 在最后一项之后,为什么不直接使用
rvCurrencies.setPadding(0, 0, 0, 100)
rvCurrencies.clipToPadding = false
或在XML
android:paddingBottom="100dp"
android:clipToPadding="false"
这样,你的两个问题就迎刃而解了。