How to Fix Android RecyclerView Not Updating
In this tutorial, you'll learn about How to Fix Android RecyclerView Not Updating. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
You update the data list but the RecyclerView stays unchanged:
dataList.add(newItem)
adapter.notifyDataSetChanged() // nothing happens
Or items render with stale data, duplicate entries, or blank views.
The Adapter is not notified of changes correctly, or the LayoutManager does not remeasure after updates.
Quick Fix
Step 1: Use notifyItemInserted instead of notifyDataSetChanged
dataList.add(newItem)
adapter.notifyItemInserted(dataList.size - 1)
notifyDataSetChanged is inefficient and can miss animations. Use targeted notifications:
| Change | Method |
|---|---|
| Item added | notifyItemInserted(index) |
| Item removed | notifyItemRemoved(index) |
| Item changed | notifyItemChanged(index) |
| Multiple changes | notifyItemRangeInserted/Removed/Changed |
Step 2: Apply all changes before notifying
fun updateData(newList: List<Item>) {
dataList.clear()
dataList.addAll(newList)
adapter.notifyDataSetChanged()
}
Clearing and notifying outside the Adapter's internal list causes inconsistencies.
Step 3: Use ListAdapter with DiffUtil
class MyAdapter : ListAdapter<Item, MyViewHolder>(DiffCallback()) {
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.bind(getItem(position))
}
}
class DiffCallback : DiffUtil.ItemCallback<Item>() {
override fun areItemsTheSame(old: Item, new: Item) = old.id == new.id
override fun areContentsTheSame(old: Item, new: Item) = old == new
}
Submit new list with <a href="/design-patterns/adapter/">Adapter</a>.submitList(newList) instead of calling notify methods manually.
Step 4: Check the LayoutManager
// Verify the LayoutManager is set
recyclerView.layoutManager = LinearLayoutManager(this)
Without a LayoutManager, the RecyclerView does not display any items. Verify this is set before the Adapter.
Step 5: Check data source reference
// Wrong - creates a new list, adapter still references the old one
dataList = fetchNewData()
adapter.notifyDataSetChanged()
// Right - mutate the existing list reference
dataList.clear()
dataList.addAll(fetchNewData())
adapter.notifyDataSetChanged()
Step 6: Call notifyDataSetChanged on main thread
runOnUiThread {
adapter.notifyDataSetChanged()
}
RecyclerView modifications must happen on the main thread. Background thread notifications are silently ignored.
Prevention
- Use
ListAdapterwithDiffUtilfor automatic, animated updates. - Always call notify methods after actual data mutations.
- Keep a single source of truth for the Adapter's data list.
Common Mistakes with recyclerview not updating
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
DodaTech Tool Reference
Doda Browser's UI Profiler captures RecyclerView rendering performance in real time, helping identify jank from incorrect notification patterns.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro