Recyclerview not call onCreateViewHolder
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
onCreateViewHolder is only called when RecyclerView actually needs a new row view. If it never fires, the problem is usually not the method itself but one of the prerequisites around the adapter, layout manager, item count, or view size. The fastest way to debug it is to check those prerequisites in order instead of guessing.
What Triggers onCreateViewHolder
RecyclerView calls onCreateViewHolder when it needs a new ViewHolder instance for a visible row. That means all of the following must be true:
- the
RecyclerViewhas aLayoutManager - the adapter is attached
- '
getItemCount()is greater than zero' - the
RecyclerViewhas enough size to lay out visible children
If any of those conditions fail, onCreateViewHolder may never run.
Start With a Minimal Working Setup
Here is a small working example in Kotlin:
And in an activity or fragment:
If your code differs materially from that shape, start there.
Check getItemCount() First
If getItemCount() returns zero, there are no items to display, so RecyclerView has no reason to create any holders.
This sounds obvious, but many cases come from loading data asynchronously and attaching an adapter before the list is populated. If the list changes later, call the appropriate notify method.
If you never notify the adapter after the data arrives, RecyclerView may still think there are zero rows.
Make Sure a LayoutManager Is Set
RecyclerView does not lay out child views on its own. Without a LayoutManager, nothing is measured or displayed.
This is one of the most common causes of "adapter methods are not being called" bugs. If onCreateViewHolder is missing, verify the layout manager before checking anything more exotic.
Confirm the View Has Size on Screen
Even with a correct adapter and nonzero item count, a RecyclerView that measures to zero height or width may not request any children.
Example XML:
If the view is inside a parent with bad constraints, hidden visibility, or zero size, onCreateViewHolder may not run because there is no visible area to fill.
Understand That Reuse Changes Call Frequency
Another source of confusion is expecting onCreateViewHolder to fire for every row every time. That is not how RecyclerView works. Once enough holders exist for the visible screen, scrolling usually reuses them through onBindViewHolder.
So if onCreateViewHolder fired only a few times, that can be completely normal. The real question is whether it never fires at all.
Fragment and Timing Issues
In fragments, adapter setup often happens before the view hierarchy is ready or after data arrives asynchronously. A safe pattern is:
Then update the adapter when data loading completes. If you replace the list inside the adapter, make sure the adapter sees the new data and receives the correct notify call.
Common Pitfalls
The most common cause is getItemCount() returning zero. Another is forgetting to set a LayoutManager, which prevents layout entirely. Developers also attach the adapter but never notify it when asynchronous data arrives. Zero-size or hidden RecyclerView layouts can produce the same symptom. Finally, some people expect onCreateViewHolder to run for every bind, but after the first few visible rows, RecyclerView mostly reuses holders and calls onBindViewHolder instead.
Summary
- '
onCreateViewHolderruns only whenRecyclerViewneeds a new visible row view.' - Verify
LayoutManager, adapter attachment, nonzero item count, and actual view size. - Check
getItemCount()early when debugging. - Notify the adapter after asynchronous data updates.
- Do not expect
onCreateViewHolderto run for every row on every scroll. - Start from a minimal known-good setup if the lifecycle seems stuck.
Related reading
- RecyclerView onClick
- RecyclerView onClick
- RecyclerView vs. ListView
- Reduce array to set in Swift
- Redirect Trace output to Console
- redis-cli connection to Amazon ElastiCache Redis cluster hangs up
- Reducing the space between sections of the UITableView
- Reference one string from another string in strings.xml?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.