Android RecyclerView with example


Android RecyclerView with example

Android RecyclerView can be said as the extension of Android Listview with more efficiency. By the term efficiency it means RecyclerView is capable handling large set of data or list item while scrolling them. It performs great by limiting the size or number of views. It is useful while loading large amount of data via network.

One of the Android Recyclerview’s advantage lies in its display. It has in-built animation that handles the scrolling smoothly. It looks more great when it is used with CoordinatorLayout.

SET UP

However, we will learn how to implement a basic Android RecyclerView to some listed data. For that, first we need to set up our build.gradle. RecyclerView doesn’t come with android by default. For that we need to add android support library for design in our project. To do that go to your build.gradle(app level) and write the following lines in dependencies section.

Also you can add dependencies separately as ‘com.android.support:design-v7:X.X.X’ is the super-set of the materials includes in it. We will be required only two things here

  • Android RecyclerView for the list
  • CardView to display cards (looks good with RecyclerView)

Here version v7:X.X.X is the version number. You can use whatever you have. We used v7:25.0.1 which is current now.

Alright, dependencies added. Next we will create our activity where we will show the RecyclerView. Here we have created RecyclerViewActivity. You can name it whatever you want.

Our layout file for RecyclerViewActivity will be

Here we have added our RecycerView in our layout. Before we move to our activity code we need to know about layout managers. Android RecyclerView comes with 3 built-in layout managers. They are –

  • LinearLayoutManager to show list with vertically or horizontally scrolling.
  • GridLayoutManager to show items in grid view (like photo albums).
  • StaggeredGridLayoutManager to show items in staggered grid view.

We will use simply LinearLayoutManager here to implement vertically scrolling.Our list will be showing some call history with Call objects. Now let’s take a look at our RecyclerViewActivity

Now we will see our Call object. We have added a java class called Call.java i.e. our call object.

Finally, the adapter to show call lists.

And the layout for every single cell in the RecyclerView

Here you can see we have used CardView which is a part of android.support.design. This makes RecyclerView more pretty.

Now just run the application and enjoy smooth scrolling of RecyclerView.

Hope you liked this article. Feel free to comment if you have any query. Share it to inspire us.

Reference : Google


Author
Abhishek is a developer cum blogger working more than 4 years. He loves programming especially open stack technologies. He has decent knowledge in Android development, Wordpress, MongoDB, Node.js and so on. Beside this Abhishek finds himself busy in painting, front-end designing.

Follow Abhishek


Previous


Android GPS automatically
Turn on GPS automatically on Android (Programmatically)

Next


Android RecyclerView Multiple ViewHolders
Android RecyclerView with multiple ViewHolders