Simple Android RecyclerView example
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
RecyclerView is the standard Android widget for rendering scrollable lists efficiently. A minimal setup needs an item layout, an adapter, a view holder, and wiring in an Activity or Fragment. This guide shows a simple Kotlin example that you can run and extend for production features like click handling and diff updates.
Add RecyclerView Dependency and Layout
Most modern Android templates already include RecyclerView through AndroidX, but verify it is available.
Create an Activity layout with one RecyclerView.
Create a Simple Row Layout
Define a row with one TextView.
Implement the Adapter and ViewHolder
Keep the first version small and readable.
Wire RecyclerView in Activity
Set a layout manager and attach adapter data.
This is enough to render a performant scrolling list.
Add Click Handling and Update Strategy
A common next step is passing a click callback into the adapter.
For dynamic lists, prefer ListAdapter and DiffUtil so UI updates are efficient and animated correctly.
Upgrade to ListAdapter for Dynamic Data
When list content changes frequently, ListAdapter with DiffUtil computes minimal updates and avoids full list redraws.
This change improves scrolling smoothness and update behavior when filters or network refreshes modify the list. It also reduces visual flicker when items are inserted, removed, or reordered during live updates.
Common Pitfalls
- Forgetting to set a
LayoutManager, which prevents items from rendering. - Inflating row layout with incorrect parent attach behavior.
- Performing heavy work inside
onBindViewHolder, causing scroll jank. - Updating backing data without notifying adapter or using diff-based adapters.
- Holding Activity references in adapters longer than needed.
Summary
- A minimal RecyclerView needs layout, adapter, view holder, and layout manager.
- Keep row binding logic light for smooth scrolling.
- Add click callbacks through adapter constructor parameters.
- Move to
ListAdapterwithDiffUtilfor real-world dynamic data. - Treat RecyclerView setup as foundation for pagination, filtering, and richer rows in production apps.
Related reading
- Simplest way to throw an error/exception with a custom message in Swift?
- Simplest way to throw an error/exception with a custom message in Swift?
- Simulate force touch / 3D touch on iPhone 6S or iPhone 6S Plus simulators
- Simulate low network connectivity for Android
- Simulator error FBSSystemServiceDomain code 4
- Since Xcode 8 and iOS10, views are not sized properly on viewDidLayoutSubviews
- single function to dismiss all open view controllers
- single function to dismiss all open view controllers
.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.