Skip to content

Most visited

Recently visited

navigation

Recycler View

Many apps need to display user-interface elements based on large data sets, or data that frequently changes. For example, a music app might need to display information about thousands of albums, but only a dozen of those albums might be on-screen at a time. If the app created UI widgets for each of those albums, the app would end up using a lot of memory and storage, potentially making the app slow and crash-prone. On the other hand, if the app created UI widgets each time a new album scrolled onto the screen and destroyed the widgets when it scrolled off, that would also cause the app to run slowly, since creating UI objects is a resource-intensive operation.

To address this common situation, the Android Support Library provides the RecyclerView suite of objects. RecyclerView and its associated classes and interfaces help you to design and implement a dynamic user interface that runs efficiently. You can use these classes as they are, or customize them to suit your specific needs.

Dynamic View Structure

Under the RecyclerView model, several different components work together to display your data. Some of these components can be used in their unmodified form; for example, your app is likely to use the RecyclerView class directly. In other cases, we provide an abstract class, and your app is expected to extend it; for example, every app that uses RecyclerView needs to define its own view holder, which it does by extending the abstract RecyclerView.ViewHolder class.

The overall container for your dynamic user interface is a RecyclerView object. You add this object to your activity's or fragment's layout; the RecyclerView, in turn, fills itself with smaller views representing the individual items. The RecyclerView uses the layout manager you provide to arrange the items. You can use one of our standard layout managers (such as LinearLayoutManager or GridLayoutManager), or implement your own.

The individual items are represented by view holder objects. These objects are instances of the class you define by extending RecyclerView.ViewHolder. Each view holder is in charge of displaying a single item, and has its own view. For example, if a RecyclerView is used to display a user's music collection, each view holder might represent a single album. In this case, the view holder's view might contain elements to display the album's title and artwork, and might respond to clicks by playing or pausing that album. The RecyclerView creates only as many view holders as are needed to display the on-screen portion of the dynamic content, plus a few extra. As the user scrolls through the list, the RecyclerView takes the off-screen views and rebinds them to the data which is scrolling onto the screen.

The view holder objects are managed by an adapter, which you create by extending the RecyclerView.Adapter abstract class. The adapter creates view holders as needed. The adapter also binds the view holders to their data. It does this by assigning the view holder to a position, and calling the adapter's onBindViewHolder() method. That method uses the view holder's position to determine what the contents should be. For example, in the case of the music collection, the adapter might call onBindViewHolder() to assign a particular view holder to position 420. The method would find out which album is in that spot in the list, and fill in the view holder's widgets with the appropriate artwork and title.

The RecyclerView model does a lot of optimization work so you don't have to:

RecyclerView Workflow

To understand how RecyclerView works, it may be helpful to follow a typical workflow. In this case, we assume that an app's main activity uses a RecyclerView to manage a simple list of text items, such as a list of names.

When the system creates the app's main activity, the system calls the activity's onCreate() method. That method takes a number of steps to set up the RecyclerView:

The RecyclerView calls the adapter to create any necessary view holders. Each view holder displays one item's data. For example, a music app might use a view holder for each album.

If the user scrolls the list, the Android Support Library brings the already-prepared view holders into view, and creates and binds new ones as necessary. It does not immediately destroy the view holders that have scrolled out of sight; they are kept available, in case the user scrolls back. However, after a certain number of view holders have been created, the Android Support Library does not create new ones. Instead, it rebinds existing view holders as needed by calling onBindViewHolder() for them, specifying a new position. That method updates the view holder's contents, but it reuses the view that was already created for that view holder. The method does not have to perform costly actions like building or inflating a new view.

If the list changes, the app calls an appropriate RecyclerView.Adapter notification method. The Android Support Library responds to the notification by rebinding any affected view holders, allowing their data to be updated. The app can improve performance by choosing a more precise notification; for example, if the data in a single item needs to be updated, the app can call notifyItemChanged(int), and the Android Support Library will just rebind that one item if it is visible.

Customizing RecyclerView

You can customize the RecyclerView objects to meet your specific needs. The standard classes provide all the functionality that most developers will need; in many cases, the only customization you need to do is design the view for each view holder and write the code to update those views with the appropriate data. However, if your app has specific requirements, you can modify the standard behavior in a number of ways. The following sections describe some of the other common customizations.

Modifying the layout

The RecyclerView uses a layout manager app to arrange the individual data items on the screen. The Android Support Library includes three standard layout managers, each of which offers many customization options:

If none of these layout managers suits your needs, you can create your own by extending the RecyclerView.LayoutManager abstract class.

Item animations

Whenever an item changes, the RecyclerView uses an animator to change its appearance. This animator is an object that extends the abstract RecyclerView.ItemAnimator class. By default, the RecyclerView uses DefaultItemAnimator to provide the animation. If you want to provide custom animations, you can define your own animator object by extending RecyclerView.ItemAnimator.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.