When I was using RecyclerView, ItemDecoration worked perfectly. For now, after moving to ListAdapter my item decorations only appears after recreating activity. Actually, I understand why this is happening. When I was using RecyclerView, I used notifyDataSetChanged() to update my list. Now I am just using internal submitList, which uses methods to update only one item(not the whole dataset). But what am I supposed to do now? Downgrade to RecyclerView and its notifyDataSetChanged()? Is there another decision?
UPD. My problem appears when I use StaggeredGridLayoutManager(2, VERTICAL). When I just use LinearLayoutManager, the problem is gone, but I need both of them(layout managers). My item decorations:
class SpacesItemDecoration(private var space: Int, private var type: String = "main_grid") : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
if (type == "main_grid") {
//grid layout with 2 columns
val layoutParams = view.layoutParams as StaggeredGridLayoutManager.LayoutParams
outRect.bottom = space
if (layoutParams.spanIndex % 2 == 0) {
outRect.right = space/2
}
else {
outRect.left = space/2
}
}
else if (type == "main_list") {
//list layout
outRect.bottom = space
}
}
}