Recycler View: What it is?

Class Overview

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 RecyclerViewwidget 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:recyclerviewv7:23.1.0

 

Recycler View in Action

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.

Design Pattern used in Recycle View

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.

Implementation

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:

To create a custom layout manager, extend theRecyclerView.LayoutManager class.

ItemAnimator

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.

Examples

The following code example demonstrates how to add the RecyclerView to a layout:

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:

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:

Switching View Using RecyclerView

RecyclerView with multiple view type

on implementing getItemViewType(), and take care of the viewType parameter in onCreateViewHolder() we can create display different views in our RecyclerView

 

Stay updated for displaying decorations, animations and heterogeneous views using RecyclerView.

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close