I’m trying to put some text dividers containing a timestamp inbetween the items of a RecyclerView with a StaggeredGridLayoutManager.
I tried using the ItemDecoration class but I think it can’t do text and also it doesn’t really work for a staggered grid.
My Item Decoration class:
class TImestampItemDecoration(context: Context, space: Int = 30) : RecyclerView.ItemDecoration() {
private val spaceInDp = dpToPx(context, space)
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
outRect.bottom = spaceInDp
if (parent.getChildAdapterPosition(view) == 0) {
outRect.top = spaceInDp
}
}
fun dpToPx(context: Context, dp: Int): Int {
return (dp * context.resources.displayMetrics.density).toInt()
}
}
adding the item decoration to the recyclerview:
binding.recyclerView.addItemDecoration(TImestampItemDecoration(requireContext()))
L00tma5ter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.