I know there are lots of threads already on this topic, but none of the given solutions worked for me so far. I\'m trying to add or update an item of a RecyclerView
Use
mItems.set(position, newItem);
instead of
mItems.add(position, newItem);
because .set
method will replace your data to particular position.
This should work:
private ArrayList<Item> mItems;
public void replaceItem(final Item newItem, final int position) {
mItems.set(position, newItem);
notifyItemChanged(position);
}
ArrayList.set()
is the way to go to replace items.
For adding items, just append them to mItems
and then go notifyDatasetChanged()
. Another way to go is to use notifyItemRangeInserted()
. Depending on where/how are you adding new items and how many of them, it might be worth it.