Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListAdapter not updating item in RecyclerView

Edit: I understand why this happens that wasn't my point. My point is that it at least needs to give a warning or call the notifyDataSetChanged() function. Because apparently I am calling the submitList(...) function for a reason. I am pretty sure people are trying to figure out what went wrong for hours until they figure out the submitList() ignores silently the call.

This is because of Googles weird logic. So if you pass the same list to the adapter it does not even call the DiffUtil.

public void submitList(final List<T> newList) {
    if (newList == mList) {
        // nothing to do
        return;
    }
....
}

I really don't understand the whole point of this ListAdapter if it can't handle changes on the same list. If you want to change the items on the list you pass to the ListAdapter and see the changes then either you need to create a deep copy of the list or you need to use regular RecyclerView with your own DiffUtill class.


The library assumes you are using Room or any other ORM which offers a new async list every time it gets updated, so just calling submitList on it will work, and for sloppy developers, it prevents doing the calculations twice if the same list is called.

The accepted answer is correct, it offers the explanation but not the solution.

What you can do in case you're not using any such libraries is:

submitList(null);
submitList(myList);

Another solution would be to override submitList (which doesn't cause that quick blink) as such:

@Override
public void submitList(final List<Author> list) {
    super.submitList(list != null ? new ArrayList<>(list) : null);
}

Or with Kotlin code:

override fun submitList(list: List<CatItem>?) {
    super.submitList(list?.let { ArrayList(it) })
}

Questionable logic but works perfectly. My preferred method is the second one because it doesn't cause each row to get an onBind call.


with Kotlin just you need to convert your list to new MutableList like this or another type of list according to your usage

.observe(this, Observer {
            adapter.submitList(it?.toMutableList())
        })

I had a similar problem but the incorrect rendering was caused by a combination of setHasFixedSize(true) and android:layout_height="wrap_content". For the first time, the adapter was supplied with an empty list so the height never got updated and was 0. Anyway, this resolved my issue. Someone else might have the same problem and will think it is problem with the adapter.


If you encounter some issues when using

recycler_view.setHasFixedSize(true)

you should definitly check this comment: https://github.com/thoughtbot/expandable-recycler-view/issues/53#issuecomment-362991531

It solved the issue on my side.

(Here is a screenshot of the comment as requested)

enter image description here


According to the official docs :

Whenever you call submitList it submits a new list to be diffed and displayed.

This is why whenever you call submitList on the previous (already submitted list), it does not calculate the Diff and does not notify the adapter for change in the dataset.