A RecyclerView Android list displays a changing collection of rows while reusing item views as the user scrolls. Unlike a one-off layout that creates every row at once, RecyclerView works with an adapter, view holders, and a layout manager to connect data to the visible interface.
This guide updates the older support-library example in this post. It outlines the key components, shows how to wire up a basic list in Java, and covers practical choices for row layouts, updates, clicks, and troubleshooting. Check your project’s Gradle and AndroidX versions before copying a dependency version from any tutorial.
1. RecyclerView Android List: How the Pieces Fit Together
1.1 RecyclerView displays a collection of item views
RecyclerView is a container for data-driven views. It asks its adapter to create item views when needed and to bind each view to the appropriate data. As rows leave the visible area, their views can be reused for later rows rather than repeatedly constructing every item.
This makes RecyclerView useful for lists whose content can be longer than the screen, and for grids that need the same data-to-view pattern. The list’s appearance and behavior still depend on your row layout and the code you provide.
1.2 Adapter, ViewHolder, and layout manager have separate jobs
The adapter supplies item views and binds values into them. A ViewHolder stores references to the child views for one row so the adapter can update them efficiently. A layout manager decides where items go, such as in a vertical sequence or a grid.
Keeping those responsibilities clear makes an implementation easier to maintain. If the list is blank, check that the adapter is attached and reports the right item count; if rows appear in the wrong arrangement, inspect the layout manager.
2. RecyclerView vs ListView
2.1 Choose RecyclerView when layout and updates need flexibility
RecyclerView is designed to support reusable item views and different layout arrangements through layout managers. You can use the same adapter-oriented approach for a vertical list, a grid, or a staggered layout, and control how item changes are presented.
That flexibility comes with more setup than a very basic list. For a small, stable screen, consider whether the extra adapter and holder structure is justified by the needs of the app.
2.2 Existing ListView screens may not need an automatic rewrite
A working legacy ListView is not necessarily broken just because RecyclerView is available. Evaluate the screen’s requirements, maintenance costs, accessibility, and expected changes before migrating. If you do migrate, test selection, click behavior, empty states, and scroll restoration rather than only confirming that rows render.
3. Add the RecyclerView Dependency
3.1 Use AndroidX rather than the retired support-library coordinates
The original sample used old com.android.support dependencies and the compile configuration. For a current AndroidX project, use the RecyclerView artifact published in the AndroidX group and declare it with your project’s supported Gradle configuration. The exact version should be selected from the official AndroidX release notes and the compatibility requirements of your build.
For example, a module may declare an AndroidX RecyclerView dependency using the implementation configuration. Avoid mixing old support-library packages and AndroidX classes within the same implementation unless your migration setup explicitly supports that transition.
3.2 Keep dependency and SDK versions compatible
Before upgrading a library, review its release notes for compile SDK, Android Gradle Plugin, and other compatibility notes. If a build fails after an update, inspect the Gradle sync output and resolve version conflicts rather than adding arbitrary versions of related libraries.
4. Create the Item Layout and Data Model
4.1 Design one reusable row
Create an XML layout for a single list item, such as a title and a short description. Give each view a descriptive resource ID and use dimensions that work across screen sizes. A card-style row is optional; RecyclerView does not require CardView, and a simple layout may be clearer for plain text.
Set meaningful content descriptions for non-text controls, preserve readable contrast, and make clickable targets large enough for comfortable use. Use the layout that supports your design instead of adding decoration that does not help users understand the list.
4.2 Model each row as data
The original sample stores a title and description in a small data object. That is a reasonable starting pattern: represent the information needed for one row in a model, then let the adapter bind it. If the data comes from a database or network request, keep loading and error states outside the view-binding code.
Prefer immutable fields or clear update methods when your data should not change arbitrarily. If the collection changes, make sure the adapter is notified in a way that matches the update you performed.
5. Build the Adapter and ViewHolder
5.1 Create a holder for row views
Define a ViewHolder subclass that receives the item view and finds its title and description views. Keep those references on the holder rather than looking them up repeatedly during every bind. This separates view lookup from the work of displaying a particular item.
5.2 Inflate and bind through adapter methods
In onCreateViewHolder(), inflate the item layout and return a holder. In onBindViewHolder(), read the item at the supplied position and place its values into the holder’s views. In getItemCount(), return the current collection size.
The position provided to a bind call is for that binding operation. For click handlers that need the current adapter position later, check that the position is still valid before using it; lists can change between a view being bound and a click occurring.
6. Connect the Layout Manager and Adapter
6.1 Set up a vertical list in the Activity
Find the RecyclerView from the activity layout, create a LinearLayoutManager, create the adapter with the current data, and attach both to the RecyclerView. A typical setup has the following shape:
RecyclerView recyclerView = findViewById(R.id.my_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new CustomAdapter(data));
Use the correct AndroidX imports for the RecyclerView and layout manager. Avoid raw casts and static Activity view fields unless there is a specific reason; keep references scoped to the screen lifecycle.
6.2 Use a grid when the content is better scanned in columns
For a grid, use a grid layout manager and choose a span count suitable for the available width. If devices have different widths, calculate or configure the column count based on the layout rather than assuming every phone should display the same number of columns.
For items with uneven heights, consider whether a staggered grid is actually useful. Grids can complicate visual scanning and accessibility if the order is unclear, so choose them based on the content rather than appearance alone.
7. Update Data and Handle Clicks
7.1 Update the collection consistently
When the dataset changes, update the collection the adapter uses and notify the adapter. For a small example, a broad data-changed notification may be enough; for larger or frequent changes, use a list-diffing approach or targeted notifications so the interface reflects the actual changes.
Avoid changing the collection from several places without a clear owner. Otherwise, the adapter’s item count and the underlying data can become inconsistent, causing missing rows or index errors.
7.2 Attach row actions with current item data
If a row has a click action, keep the event handling close to the adapter or pass a callback from the screen. Read the item associated with the holder at the time of the click, validate its current position when needed, and avoid storing a position that can become stale after a list update.
8. Troubleshoot Common RecyclerView Problems
8.1 RecyclerView appears blank
Check that the RecyclerView has a nonzero size, that a layout manager has been set, and that the adapter is attached. Confirm that getItemCount() returns a positive value when data exists and that the row layout does not make its contents invisible or zero-sized.
8.2 Rows show stale or incorrect content
RecyclerView reuses item views, so bind every view property that can vary between rows. For example, if a row sometimes shows a badge, set the badge’s visibility for both the visible and hidden cases during binding. Otherwise, a recycled view can retain a value from a previous item.
Also verify that the adapter reads from the intended collection and that notifications follow modifications. Test with enough rows to scroll and reuse views; a list that only displays its first few items may not expose recycling mistakes.
9. Practical Checklist Before Shipping
- Use AndroidX dependencies compatible with the project’s Gradle and SDK setup.
- Give the list an appropriate layout manager.
- Keep row data separate from view code.
- Bind every state that may change when a view is recycled.
- Handle empty, loading, and error states deliberately.
- Test scrolling, clicks, screen rotation, and dataset changes.
- Check accessibility labels and touch target behavior.
For official implementation details, consult the Android Developers guide to creating dynamic lists with RecyclerView and the AndroidX RecyclerView release notes.
FAQ
What is RecyclerView used for in Android?
RecyclerView displays data as a collection of reusable item views. It can present lists and grids, with an adapter connecting data to each row and a layout manager arranging those rows.
Is RecyclerView better than ListView?
RecyclerView offers a flexible layout-manager and holder-based structure, but a migration is not automatically necessary for every existing screen. Choose based on layout needs, update behavior, and maintenance goals.
What does a RecyclerView adapter do?
The adapter creates item holders, binds data to their views, and reports how many items are in the collection. The RecyclerView uses those methods to display the appropriate rows.
Why is my RecyclerView not showing items?
Common causes include a missing layout manager, an unattached adapter, a zero item count, or a layout with no usable size. Check those first, then inspect binding and view visibility.
Do I need CardView for a RecyclerView list?
No. CardView is an optional presentation choice. A regular item layout works fine when the design does not need a card surface or elevation.
Conclusion
A well-structured RecyclerView Android list uses a layout manager to arrange rows, an adapter to connect data, and a ViewHolder to retain each row’s view references. Use AndroidX dependencies in modern projects and update the original legacy sample rather than copying its old package names unchanged.
Start with a small dataset, test scrolling and updates, and then add the interaction and layout complexity your app actually needs. The official Android guide and release notes are the best place to confirm details for your project’s current versions.




Write a comment