download

Why does inserting one RecyclerView row drop frames and kill animations?

Mobile~8 minReviewed 3 Dec 2026Free · No sign-in

Question

A 60 fps (16.67 ms frame budget) Android chat feed displays 12 visible rows out of 200 messages. Each onBindViewHolder call takes 2.4 ms. Every new message runs:

"KOTLIN"
class FeedAdapter : RecyclerView.Adapter<MsgVH>() {
    private val items = mutableListOf<Msg>()
    fun prepend(msg: Msg) {
        items.add(0, msg)
        notifyDataSetChanged()
    }
}

Inserting 1 message stalls the UI thread for 28.8 ms, drops a frame, flashes all 12 rows, and skips animations. A teammate switches to ListAdapter, mutates msg.isRead = true in place, and calls submitList(items). Now read badges stop updating.

  1. Why does notifyDataSetChanged() drop frames and kill animations?
  2. Fix the adapter with ListAdapter and DiffUtil.ItemCallback, and explain the in-place mutation bug.
Show hints (3)
  1. "COUNT BOUND ROWS"

    notifyDataSetChanged() tells RecyclerView every row is invalid. Rebinding all 12 visible rows costs 28.8 ms, exceeding the 16.67 ms frame budget.

  2. "DIFF OFF MAIN THREAD"

    ListAdapter runs DiffUtil.ItemCallback on a background thread and dispatches targeted insert, move, or change events so only affected rows rebind.

  3. "AVOID MUTATING IN PLACE"

    submitList() skips work if the new list is the same object reference. Even on a shallow copy, mutating an item in place makes areContentsTheSame compare it against itself.

Show checked explanation

notifyDataSetChanged() invalidates the whole viewport, rebinding all 12 visible rows (28.8 ms > 16.67 ms) and disabling item animations. Use ListAdapter with DiffUtil.ItemCallback (areItemsTheSame by stable ID, areContentsTheSame by immutable data class fields) so diffing runs off-main and binds only 1 row (2.4 ms). Always pass a new list of copied immutable items to submitList().

Frame-budget math (60 Hz and 120 Hz), targeted diffing, and submitList reference/in-place mutation behavior were simulated on CPython 3.12.10 (no Android SDK or Kotlin toolchain installed locally); Kotlin and RecyclerView APIs checked against official Android documentation. Checked by an editor other than the author.

Walkthrough

At 60 fps, Android gives the main thread 16.67 ms to measure, lay out, bind, and draw each frame. Calling notifyDataSetChanged() discards all structural change information. RecyclerView assumes every visible row changed, skips DefaultItemAnimator, and rebinds all 12 rows on screen: 12 * 2.4 ms = 28.8 ms. That blows past 16.67 ms and drops 1 full frame.

update strategydiff threadrows bound on 1 insertmain-thread bindanimations
notifyDataSetChanged()none12 visible rows28.8 ms (jank)disabled
DiffUtil.calculateDiff() on UImain thread1 new row2.4 ms + diff costkept
ListAdapter (AsyncListDiffer)background1 new row2.4 ms (smooth)kept

ListAdapter wraps AsyncListDiffer. It computes the minimal diff on a background executor and dispatches notifyItemRangeInserted(0, 1) on the main thread. Only the 1 inserted row binds (2.4 ms), while the other 11 bound ViewHolder instances shift smoothly with insert animations.

"KOTLIN"
data class Msg(val id: Long, val body: String, val isRead: Boolean)

class FeedAdapter : ListAdapter<Msg, MsgVH>(Diff) {
    override fun onBindViewHolder(h: MsgVH, pos: Int) {
        h.bind(getItem(pos))
    }

    object Diff : DiffUtil.ItemCallback<Msg>() {
        override fun areItemsTheSame(a: Msg, b: Msg) = a.id == b.id
        override fun areContentsTheSame(a: Msg, b: Msg) = a == b
    }
}

val next = current.map {
    if (it.id == targetId) it.copy(isRead = true) else it
}
adapter.submitList(next)

Why mutating in place breaks submitList(): First, AsyncListDiffer.submitList(newList) returns early when newList === oldList (same list reference). Second, even if you pass items.toList() as a shallow copy, mutating msg.isRead = true changes the shared object inside both snapshots. areContentsTheSame(oldItem, newItem) then compares the mutated object to itself, returns true, and skips rebinding. Immutable data class properties (val) with .copy() prevent both bugs.

Common mistakes

  • Calling notifyDataSetChanged() after inserting or toggling one row. It rebinds every visible row, drops frames, and kills insert, move, and remove animations.
  • Mutating a MutableList or item fields in place before submitList(). Same-reference lists short-circuit, and shared mutated items trick areContentsTheSame into returning true.
  • Running DiffUtil.calculateDiff() synchronously on the main thread for large feeds. The diff calculation itself can stall frames before binding even starts.
  • Comparing full item equality inside areItemsTheSame instead of a stable ID. Every field edit looks like a delete plus insert, causing rows to flash.

Follow-up questions

  • "When should you override getChangePayload() in DiffUtil.ItemCallback?" Override it when one small field changes on an expensive row, such as a read badge or like count. Returning a lightweight payload routes to the three-argument onBindViewHolder(holder, pos, payloads) so you update only that badge without rebinding images or flashing the row.
  • "Why can capturing the position parameter inside a click listener in onBindViewHolder act on the wrong row?" Inserts and deletes shift adapter positions without rebinding untouched ViewHolder instances. Read holder.bindingAdapterPosition inside the click handler, guard against RecyclerView.NO_POSITION, and fetch getItem(pos) at click time.
  • "On a 120 Hz display, what happens if binding one complex row still takes 9 ms?" A 120 Hz screen shrinks the frame budget to 8.33 ms, so even 1 heavy bind (9 ms) misses a frame. Flatten row layouts, avoid allocating objects during bind, and share a RecycledViewPool across nested lists.

What a strong answer covers

Computes the 28.8 ms vs 16.67 ms frame overrun, replaces notifyDataSetChanged() with ListAdapter plus DiffUtil.ItemCallback, separates stable ID from content equality, and explains both halves of the submitList() in-place mutation trap.

Open in interactive practice deck →

Original OfferVibe question. Create dynamic lists with RecyclerView · ListAdapter reference · DiffUtil.ItemCallback · Slow rendering.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.