With the Android 5.0 (Lollipop) release a new component called RecyclerView was introduced. RecyclerView is a replacement for the traditional ListView which promises a more flexible approach to implementing smoothly scrolling lists in Android apps. In this blog post, we'll walk through a basic RecyclerView implementation as well as highlight some of the features that this component provides.

A RecyclerView is relatively simple to set up. Let's go through it step by step...

First, we need to include the support library as an app dependency. RecyclerView is included in the v7 support libraries, so we'll need to include the dependency in our app module depenencies:

dependencies {
...
compile 'com.android.support:recyclerview-v7:22.0.+'
}

Next add the RecyclerView to your layout xml:

android:id="@+id/shopping_list_rv" android:layout_width="fill_parent" android:layout_height="fill_parent"/>

Next, we'll need to add a reference to the recycler view in our activity so that we can supply it with some data. In the onCreate() method of our activity, we first need to obtain a reference to the recycler view from our layout file.

private RecyclerView mRecyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_list);
...

mRecyclerView = (RecyclerView)findViewById(R.id.shopping_list_rv);

The next step is to pick a LayoutManager implementation (or define our own). The layout manager is responsible for positioning the items within the RecylerView and determining when the items are no longer visible to the user. The layout manager can reuse (recycle) any views that are no longer visible to the user by replacing the views' content with new data from the adapter. There are three default layout manager implementations:

1. LinearLayoutManager: Displays items in a scrollable vertical or horizontal list (Similar to a traditional ListView).

2. GridLayoutManager: Displays intems in a two dimentional grid (Similar to a GridView).

3. StaggeredGridLayoutManager: Displays items in a staggered grid.

You can also implement your own layout manager by extending the RecylerView.LayoutManager class.

For now, since we just want to show a list of items, we'll just choose the LinearLayoutManager.

mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

Now that we have a layout manager, it's time to implement a list adapter. We will implement our own list adapter which extends the RecyclerView.Adapter abstact class.

public class ShoppingListItemListAdapter extends RecyclerView.Adapter {
private List mItemList;
public ShoppingListItemListAdapter(List itemList) {
mItemList = itemList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.basic_list_item_layout, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ShoppingListItem item = mItemList.get(position);
holder.mItemNameView.setText(item.getName());
}
@Override
public int getItemCount() {
return mItemList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView mItemNameView;
public ViewHolder(View itemView) {
super(itemView);
mItemNameView = (TextView) itemView.findViewById(R.id.name_text_view);
}
}
}

The important part to note here is the use of the View Holder pattern. The view holder pattern is a performance optimization that can be implemented for any list view, but RecyclerView forces us to use it. The ViewHolder object stores each subview inside the layout's tag field so that the views don't need to be looked up in order to display every row in the list view. This pattern improves performance dramatically, especially with larger lists.

There you have it, thats a quick overview of the the Recycler View component. It's a simple component to implement and it's use of the View Holder pattern leads to better performance for scrolling list views.