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()))
I solved it myself. I created a different Item view with just a text view and submitted it to the viewHolder via submitList(). I then did this to make it occupy the whole width of the recyclerview:
val layoutParams: StaggeredGridLayoutManager.LayoutParams? =
viewHolder.itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams?
layoutParams!!.isFullSpan = true