0
votes

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
        }

    }
}
1
This sounds like an issue somewhere else since you say recreating the Activity fixes it. I use item decorations with ListAdapter and it works fine.Tenfour04
What functionality you want to achieve? Show divider?Sam Chen
@Tenfour04 It works good only when I use LinearLayoutManager. But I need StaggerGridLayoutManager too. I updated the question.Viktor
@SamChen I show list of elements with StaggerGridLayoutManager(2 columns) and LinearLayoutManager. I have a problem with the first case, but not with the second one. Updated the question.Viktor

1 Answers

0
votes

Actually, it's a common problem of RecyclerView and ListAdapter. So, my problem is the same as described here and here. This solution helped me with the bug only when all elements of ListAdapter are visible. If there are more elements than fit on the screen, the bug appears again(it's a little bit different, but still with the decorations). So, the only way for me - downgrading to RecyclerView + using notifyDataSetChanged(). It's bad practice, but anyway.