Updated 14 December 2016
A flexible view for providing a limited window into a large data set.
RecyclerView is an extremely powerful and flexible way to show a list, grid, or any view of a large set of data. One advantage over ListView
or GridView
is the built in support for animations as items are added, removed, or repositioned. It is supposed to be the successor of ListView and GridView, and it can be found in the latest support-v7 version.
This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views. Use the RecyclerView
widget when you have data collections whose elements change at runtime based on user action or network events.
The RecyclerView
class simplifies the display and handling of large data sets by providing:
The RecyclerView has been developed with extensibility in mind, so it is possible to create any kind of layout we can think of that contains data displayed in the form of tiles.
The Gradle build script dependency identifier for this library is as follows:
com.android.support:recyclerview–v7:23.1.0
So RecyclerView
is the appropriate view to use when you have multiple items of the same type and it’s very likely that your user’s device cannot present all of those items at once. Possible examples are contacts, customers, audio files and so on. The user has to scroll up and down to see more items and that’s when the recycling and reuse comes into play. As soon as a user scrolls a currently visible item out of view, this item’s view can be recycled and reused whenever a new item comes into view.
Now, you might say: That’s nothing new. And you’re right! We had that with ListView
for a very long time. The concept of recycling views itself it not new. But while you previously had a ListView
where the appearance, recycling and everything was tightly coupled, Google now follows a much better, a much more flexible approach with the newRecyclerView
.
Android created RecycleView as a ListView improvement, using RecyclerView we can:
1.) Reuse cells while scrolling up/down – this is possible with implementing View Holder in the listView adapter, but it was an optional thing, while in the RecycleView it’s the default way of writing adapter.
2.) Decouple list from its container – so we can put list items easily at run time in the different containers (linearLayout, gridLayout) with setting LayoutManager
3.) Animate common list actions – Animations are decoupled and delegated to ItemAnimator.
We all know how to use ListView in our app and we know if we want to increase the ListView performances we can use a pattern called ViewHolder. This pattern consists of a simple class that holds the references to the UI components for each row in the ListView. This pattern avoids looking up the UI components all the time the system shows a row in the list. Even if this pattern introduces some benefits, we can implement the ListView without using it at all. RecyclerView forces us to use the ViewHolder pattern.
To use the RecyclerView
widget, you have to specify an adapter and a layout manager. To create an adapter, extend the RecyclerView.Adapter
class. The details of the implementation depend on the specifics of your dataset and the type of views.
A layout manager positions item views inside a RecyclerView
and determines when to reuse item views that are no longer visible to the user. To reuse (or recycle) a view, a layout manager may ask the adapter to replace the contents of the view with a different element from the dataset. Recycling views in this manner improves performance by avoiding the creation of unnecessary views or performing expensivefindViewById()
lookups.
RecyclerView
provides these built-in layout managers:
LinearLayoutManager
shows items in a vertical or horizontal scrolling list.GridLayoutManager
shows items in a grid.StaggeredGridLayoutManager
shows items in a staggered grid.To create a custom layout manager, extend theRecyclerView.LayoutManager
class.
ItemAnimator will animate ViewGroup modifications that are notified to adapter. Basically it will automatically animate adding and removing items. That’s not an easy class either, and we find aDefaultItemAnimator that works quite well.
The following code example demonstrates how to add the RecyclerView
to a layout:
1 2 3 4 5 |
<android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" /> |
Once you have added a RecyclerView
widget to your layout, obtain a handle to the object, connect it to a layout manager, and attach an adapter for the data to be displayed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class MyActivty extends Activity { private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter; private RecyclerView.LayoutManager mLayoutManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_activity); mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); // use this setting to improve performance if you know that changes // in content do not change the layout size of the RecyclerView mRecyclerView.setHasFixedSize(true); // use a linear layout manager mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); // specify an adapter mAdapter = new MyAdapter(myDataset); mRecyclerView.setAdapter(mAdapter); } ... } |
The adapter provides access to the items in your data set, creates views for items, and replaces the content of some of the views with new data items when the original item is no longer visible. The following code example shows a simple implementation for a data set that consists of an array of strings displayed using TextView
widgets:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private String[] mDataset; // Provide a reference to the views for each data item // Complex data items may need more than one view per item, and // you provide access to all the views for a data item in a view holder public static class ViewHolder extends RecyclerView.ViewHolder { // each data item is just a string in this case public TextView mTextView; public ViewHolder(TextView view) { super(view); mTextView = view; } } // Provide a suitable constructor (depends on the kind of dataset) public MyAdapter(String[] myDataset) { mDataset = myDataset; ... } // Create new views (invoked by the layout manager) @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // create a new view or inflate your custom layout to display as a each item in recycle view View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_text_view, parent, false); // set the view's size, margins, paddings and layout parameters ... ViewHolder vh = new ViewHolder(view); return vh; } // Replace the contents of a view (invoked by the layout manager) @Override public void onBindViewHolder(ViewHolder holder, int position) { // - get element from your dataset at this position // - replace the contents of the view with that element holder.mTextView.setText(mDataset[position]); } // Return the size of your dataset (invoked by the layout manager) @Override public int getItemCount() { return mDataset.length; } } |
1 2 3 |
public void viewSwitcher(View view) { recyclerView.setLayoutManager(new GridLayoutManager(this, 2)); } |
on implementing getItemViewType(), and take care of the viewType
parameter in onCreateViewHolder() we can create display different views in our RecyclerView
1 2 3 4 |
@Override public int getItemViewType(int position) { return viewType; } |
1 2 3 4 5 6 7 8 |
@Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { switch (viewType) { case 0: return new ViewHolder0(...); case 2: return new ViewHolder2(...); ... } } |
Stay updated for displaying decorations, animations and heterogeneous views using RecyclerView.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.