I solved this by changing my removeItemAt()
method as follows:
private void removeItemAt(int position) {
if (list.size() > 0) {
list.remove(position);
notifyItemRemoved(position);
if (position != 0) {
notifyItemChanged(position - 1, Boolean.FALSE);
}
}
}
The second argument to notifyItemChanged()
is the key. It can be literally any object, but I chose Boolean.FALSE
because it's a "well-known" object and because it conveys a little bit of intent: I don't want animations to run on the item I'm changing.
Why it works
It turns out that there's a second onBindViewHolder()
method defined in RecyclerView.Adapter
that I'd never seen before. From the source code:
public void onBindViewHolder(VH holder, int position, List<Object> payloads) {
onBindViewHolder(holder, position);
}
Excerpted from that method's documentation:
The payloads
parameter is a merge list from notifyItemChanged(int, Object)
or notifyItemRangeChanged(int, int, Object)
. If the payloads
list is not empty, the ViewHolder
is currently bound to old data and Adapter
may run an efficient partial update using the payload info. If the payload is empty, Adapter
must run a full bind.
In other words, by passing something (anything) as a payload in notifyItemChanged()
, we're telling the system that we want to perform only a "partial update" (in situations where that's possible).
So, sure, we've instructed the system to perform a partial update... but how does that stop the flickering caused by simply calling notifyItemChanged(position - 1)
? It has to do with the RecyclerView.ItemAnimator
that's attached to all RecyclerView
s by default: DefaultItemAnimator
.
DefaultItemAnimator
's source code includes this method:
@Override
public boolean canReuseUpdatedViewHolder(@NonNull ViewHolder viewHolder,
@NonNull List<Object> payloads) {
return !payloads.isEmpty() || super.canReuseUpdatedViewHolder(viewHolder, payloads);
}
You'll notice that this method also takes a List<Object> payloads
parameter. This is the same list of payloads as used in the other onBindViewHolder()
call mentioned above.
Because we passed a payload argument, this method will return true
. And since the animator is now told that it can reuse the already-existing ViewHolder
for our "changed" item, it doesn't tear it down and create a new one (or reuse a recycled one)... which stops the default fade animation on the changed item from running!