In a RecyclerView whose rows show live download progress, why does each ListAdapter update make the row flicker, and how do change payloads fix it?
answer
- two holders for one change
- DefaultItemAnimator cross-fades
- getChangePayload says what changed
- three-argument onBindViewHolder
- empty payload list means full bind
basics
~20 sWithout a payload, DefaultItemAnimator binds the change into a second ViewHolder and cross-fades the two, so each progress tick flashes the row. Returning a payload from getChangePayload lets RecyclerView reuse the same holder for a partial bind.
solid answer
~30 sA content change reaches RecyclerView as `notifyItemRangeChanged`. The default `DefaultItemAnimator` reuses the existing holder only when the payload list is non-empty or change animations are off; otherwise RecyclerView binds the new data into a second holder and `animateChange` cross-fades them, which on a per-second progress update looks like flicker. Override `DiffUtil.ItemCallback.getChangePayload` to return a marker when only progress changed, and handle it in `onBindViewHolder(holder, position, payloads)` by updating just the progress bar; an empty list still means a full bind. Payloads can be merged or dropped, so the full bind must always be correct on its own.
code
kotlin · 21 linesprivate const val PROGRESS_CHANGED = "progress"
object BookDiff : DiffUtil.ItemCallback<Book>() {
override fun areItemsTheSame(oldItem: Book, newItem: Book) = oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: Book, newItem: Book) = oldItem == newItem
override fun getChangePayload(oldItem: Book, newItem: Book): Any? =
if (oldItem.copy(downloadProgress = newItem.downloadProgress) == newItem) {
PROGRESS_CHANGED
} else {
null
}
}
// In the ListAdapter<Book, BookViewHolder>
override fun onBindViewHolder(holder: BookViewHolder, position: Int, payloads: MutableList<Any>) {
if (payloads.isNotEmpty() && payloads.all { it == PROGRESS_CHANGED }) {
holder.progressBar.progress = getItem(position).downloadProgress
} else {
onBindViewHolder(holder, position)
}
}go deeper
Remember that RecyclerView animates item changes by default and that a payload tells the adapter only part of a row changed.
Explain getChangePayload, the three-argument onBindViewHolder, and why an empty payload list means a full bind.
Trace the flicker to DefaultItemAnimator's cross-fade of two holders, prove it by disabling change animations, and fix it with payloads plus a correct full bind.
Decide when high-frequency row updates belong in the list's data at all, and standardise payloads for them rather than turning animations off list by list.
## The symptom An audiobook library shows a download progress bar on each book row. Every second the ViewModel emits a new list with updated percentages, and `ListAdapter.submitList` dispatches the changes. On screen, the rows that are downloading **flash or flicker on every tick**, and a tap that lands mid-animation sometimes feels ignored. Nothing is broken in the data; the problem is how RecyclerView animates a change. ## Why a plain change flickers When DiffUtil finds an item whose `areItemsTheSame` is true but `areContentsTheSame` is false, `ListAdapter` dispatches `notifyItemRangeChanged(position, count, payload)`. What RecyclerView does next is up to the **`ItemAnimator`**, by default a `DefaultItemAnimator`: 1. RecyclerView asks the animator `canReuseUpdatedViewHolder(holder, payloads)`. 2. `DefaultItemAnimator` answers **true when the payload list is not empty**. Otherwise it defers to `SimpleItemAnimator`, which answers true only when change animations are off (`setSupportsChangeAnimations(false)`) or the holder is invalid. Change animations are **on by default**. 3. If the answer is false, RecyclerView binds the new data into a **second ViewHolder** and gives both to `animateChange`, and `DefaultItemAnimator` **cross-fades** the old row out and the new one in. So with no payload, every progress tick creates a cross-fade between two copies of the row. At one tick per second that reads as a constant flicker, and while the animation runs the row is two views. ## The fix: a change payload A **payload** is any object you attach to a change to say what changed. Two pieces are needed: - **`DiffUtil.ItemCallback.getChangePayload(oldItem, newItem)`** returns a marker (for example a constant meaning "progress only") when the only difference is the progress. It is called only for items that are the same with different contents, and the default returns `null`. - **`onBindViewHolder(holder, position, payloads)`**, the three-argument overload, handles a non-empty payload list with a **partial bind**: update the progress bar and nothing else. An empty list means a full bind, and the default implementation of the overload simply calls the two-argument `onBindViewHolder`. With a non-empty payload, `DefaultItemAnimator` reuses the same holder. Its documentation says that if you do not override `animateChange`, it runs a move animation instead of a cross-fade. The row stays one view, nothing cross-fades, and only its progress bar redraws. ## Rules the source states about payloads | Rule | Consequence | |---|---| | Payloads arrive as a **merged list** | several ticks before the next frame give several entries; handle each or take the latest item from `getItem(position)` | | A change with a `null` payload marks the holder for a **full update** | any null-payload change in the same pass empties the list you receive | | Payloads **may be dropped** | the javadoc says a payload for a view that is not attached is simply dropped, so a full bind must always be correct on its own | | `canReuseUpdatedViewHolder` is only asked when the **view type is unchanged** | a change of view type always gets two holders | That third rule is the one people miss: the partial bind is an optimisation, never the only path that sets the progress bar. ## Alternatives and when they fit - **`(recyclerView.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false`** stops the cross-fade for every change in that list. Simple, but it also removes change animations you might want elsewhere. - **Updating the view directly from outside the adapter**, for example finding the holder and setting the bar, bypasses the adapter's data and goes stale the moment the row is recycled. - **`notifyDataSetChanged()` on each tick** rebinds and relays out every visible row and drops animations entirely; it trades a flicker for wasted work. Payloads are the precise tool: the diff states what changed, the bind touches only that view, and everything else (inserts, removals, moves) keeps its animation. ## How to confirm the diagnosis 1. Temporarily turn change animations off. If the flicker disappears, the cross-fade was the cause. 2. Log the payload list in the three-argument bind. An always-empty list means `getChangePayload` returns `null` or a null-payload change is merged in. 3. Check that `areItemsTheSame` compares ids. If it compares whole objects, a progress change looks like a removal plus an insertion, and payloads never come into play. Measuring frame times with a benchmark or a profiler is covered in the lessons on those tools.
- Why must a RecyclerView row's full bind still set the progress bar when a payload path exists?The javadoc says an adapter should not assume a payload reaches `onBindViewHolder`: a payload for a view that is not attached is simply dropped, and a null-payload change in the same pass turns the bind into a full update with an empty list. The full bind is the path that must always be correct; the payload path is an optimisation.
- When is setSupportsChangeAnimations(false) a better fix than payloads?When the list has no change animation worth keeping and you want one line instead of a payload protocol. It makes `SimpleItemAnimator` reuse the holder for every change, so nothing cross-fades. It still rebinds the whole row on each change, so payloads remain the cheaper option when only one view changes often.
- Does RecyclerView ask the ItemAnimator about reusing the holder when an item's view type changes?No. The javadoc says `canReuseUpdatedViewHolder` is only called when the holder still has the same view type; otherwise the animator always receives two holders in `animateChange`. A change of view type cannot be a partial bind.
- Change animations are off by default, so the flicker must come from the data layer
- Payloads always reach onBindViewHolder, so the full bind can skip the progress bar
- An empty payload list means nothing changed and the bind can return early
- Calling notifyDataSetChanged on each tick is the fix because it skips animations
- getChangePayload is called for every item in the new list