Recycler view in Android


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 need.
                     
                             

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.


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 onCreate() method creates the RecyclerView specified by activity's layout:

                myRecyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);

  • The onCreate() method creates an adapter; the adapter class is specified by the app, extending the Android Support Library's RecyclerView.Adapter class. Typically, when the app creates the adapter, the app passes the information the adapter needs to get its data.

                myAdapter = new CustomAdapter(myDataSet);

The adapter's constructor does any necessary initialization. For example, it might create an inflater to use for inflating the layouts of its view holders.

  • The onCreate() method attaches the adapter to the RecyclerView.

                  myRecyclerView.setAdapter(myAdapter);

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.

  • The adapter class's definition specifies which class it uses as its view holder:

              public class CustomAdapter extends RecyclerView.Adapter<CustomViewHolder>

The Android Support Library calls the adapter's onCreateViewHolder() method. That method needs to construct the view holder and set the view it uses to display its contents. Typically, it would set the view by inflating an XML layout file. Since the view holder is not yet assigned to any particular data, the method does not actually set the view's contents.

// Inflate the view for this view holder
View thisItemsView = myInflater.inflate(R.layout.list_item_layout,
        parent, false);
// Call the view holder's constructor, and pass the view to it;
// return that new view holder
return new CustomViewHolder(thisItemsView);

The Android Support Library then binds the view holder to its data. It does this by calling the adapter's onBindViewHolder() method, and passing the view holder's position in the RecyclerView. The onBindViewHolder() method needs to fetch the appropriate data, and use it to fill in the view holder's layout. For example, if the RecyclerView is displaying a list of names, the method might find the appropriate name in the list, and fill in the view holder's TextView widget.

@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
  // Find out the data, based on this view holder's position
     String thisItemsName = myNameList.get(position);
     holder.nameTextView.setText(thisItemsName);
}

The Android Support Library repeats this process, creating and binding new view holders until the visible portion of the RecyclerView is filled. It also creates
and binds one or two more, so if the user scrolls the list, the new view holders are ready to display.