RecyclerView.ItemDecoration doesn't update after item is removed from RecyclerView.Adapter

后端 未结 3 534
小鲜肉
小鲜肉 2020-12-10 03:28

My question

How can I get my ItemDecoration to \"update\" the item offsets for other views when I remove a given view from my adapter and the

3条回答
  •  [愿得一人]
    2020-12-10 04:06

    ListAdapter solution:

    listAdapter.submitList(newList) {
        handler.post { // or just "post" if you're inside View
            recyclerView.invalidateItemDecorations()
        }
    }
    

    The key is new submitList(list, commitCallback) method from latest RecyclerView release. It allows you to call invalidateItemDecorations() after ListAdapter will apply all changes to RecyclerView.

    I also had to add post {} call to make it work - without it decorations invalidated too early, and nothing happens.

提交回复
热议问题