Android RecyclerView

Android RecyclerView

What is RecyclerView?

Google’s upcoming operating system named Android L looks very promising. It is highly focused on rich user experience and what they called it as material design. In this example we will take a look at the new UI widget called RecyclerView.

RecyclerView is more advanced and flexible and efficient version of ListView. RecyclerView ViewGroup is an container for larger data set of views that can be recycled and scrolled very efficiently. RecyclerView can be used for larger datasets to be rendered on the UI like a list. RecyclerView provides maximum flexibility to design different kind of views.

Just like ListView, RecyclerView is used to display a large amount of similar items on screen. But since the Android team was building an enhancement, they added a bunch of new features to RecyclerView. Each one of these new features, give a platform to developers for implementing a highly custom made RecyclerView. One of the custom implementations of RecyclerView is the new Gmail app on Android.

Example

In this tutorial, we are going to learn how to use RecyclerView to show items in List RecyclerView and Grid RecyclerView.

VIDEO DEMO

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. Android SDK doesn’t includes the RecyclerView class, and hence for using RecyclerView in your project you first need to add the recycler view support library to your project. You can download recyclerview library from here.

3. Firstly create a layout naming recycler_view.xml for using this layout in this tutorial as we had done in Toolbar tutorial.

4. As you had seen in above video i am using tabs for displaying list and grid items. So i am using viewpager for this in activity_main.xml. If you are not aware of the ViewPager then you can refer to my ViewPager tutorial.

5. Now, we need to create two more layout for List and Grid items. This layouts will be used in fragments for tabs. If you are not aware of fragments then you can refer to Fragments tutorial.

In both layouts i am including recyclerview layout that i had created above. You can use single layout for both fragments.

6. Now, we have to create another two more layouts for both List and Grid custom layouts. This layouts are going to be used in recyclerview adapter for inflating over recycler view.

Since, both List and Grid are different in layout so they will need different custom layout.

List Custom View xml Layout.

Grid Custom View xml Layout.

7. Open res ⇒ values ⇒ strings.xml and add below string values. This are some strings that i am going to use in this tutorial and there are String Array also that is for items data.

8. Before moving further, we have to create a ViewHolder for both List and Grid recyclerview. For this we have to two new java classes. In this classes we have to extend the class with RecyclerView.ViewHolder.  In this classes i had used click listener for implementing click listener over recycler view items. We have to setClickListener for all views which we want to do something on click.

9. Now, create a getter and setter class for recyclerView. As we use to create in ListView and GridView.

10. Android RecyclerView included special kind of adapter which works pretty much same as traditional Android adapters but with additional functionalities. The additional functionalities inclued :

  • It adds two new methods like onCreateViewHolder() and onBindViewHolder() to organize the code. You must override these two methods for inflate the view and to bind data to the view
  • Implements a ViewHolder by default. Conceptually RecyclerView.ViewHolder works same as the ViewHolder design pattern which we have been using with other Adapters.
  • Takes care of the overhead of recycling and gives better performance and scrolling.

This is onCreateViewHolder method for List RecyclerView

This is onBindViewHolde method for List RecyclerView in this method we have to set items over views.

ListRecycelrView Adapter Class.

GridRecyclerView Adapter Class.

12. Handling click event on RecyclerView is not as sweet as handling click listener in ListView or GridView. Android RecyclerView doesn’t provide any built in listeners or handy way of handling click events.

For this I created an interface class for onClickListener you can implement this interface in all ViewHolder and implement clickListener.

Now we can use this interface to handle click event explicitly by adding the following code in onBindViewHolder() method. As we had done in above adapter classes.

13. RecyclerView having two LayoutManagers for showing data. LinearLayoutManager for List items and GridLayoutManager for Grid items. The below code is for List items using LinearLayoutManager and LayoutManager is neccessary in RecyclerView.

Full Code of List RecyclerView fragment.

Full Code of Grid RecyclerView fragment.

In above both fragment classes i am adding items dynamically by using a RandomGenerator method.

14. Finally, come to MainActivity.java and add the following code to set tab fragment over ViewPager.

15. Now, you are all done, run your app and you will get the output as shown in video.

Thanks. :)

Download Source Code – >

 

 

 

COMMENTS (2)
Reply

Thanks for sharing your knowledge with everyone, I wanted to ask how I can place a spinner in a recyclcerview, you have some tutorial or link about this, you appreciate too.

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)