Replace your existing ListView with RecyclerView

With my new app I’ve been playing with the new Material Design elements. It’s going to be a small fun game and it’s perfect for experimental causes. I implemented a leaderboard in the app using a ListView, but then I thought I could replace it with the RecyclerView. The RecyclerView is said to handle large data sets way more efficiently than the ListView and it provides great customization and animation solutions. It focuses on recycling which makes the addition and removal of items more efficient. If your list is not dynamic or it’s not populated from the cloud then there’s no reason to replace your existing ListView with RecyclerView.

I’m going to focus on the code and ignore much talk because I myself like getting straight to the point instead of reading long blog posts.

My ListView currently includes three TextViews and an ImageView. This is how it’s built up:

Layout

List item layout

Code

Put this code where you finish populating your arrays:

This is your Adapter class:

Your ListView implementation should be very similar to mine. Populate the arrays some way (in my case I download the data from the cloud), and bind these to the adapter.

Here comes the fun part. Our RecyclerView implementation is approx. 20% different :)

Gradle

Add this to your build.gradle:

If you have any issues, rebuild and clean the project. Don’t forget to download the support library in the SDK Manager.

Layout

Define the RecyclerView

Use setHasFixedSize when you know that the RecyclerView’s width and height won’t change. Whenever items are inserted, moved or removed the size (width and height) of RecyclerView might change. The list items can also have variable width or height by default. If your app lacks these operations, RecyclerView will be better optimized as knows the exact size in advance based on your adapter.

Unlike ListView, a RecyclerView needs a LayoutManager to manage positioning of its items and to detertime when it’s time to recycle the views. There are 3 types of the LayoutManager as LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager. Now let’s stay with LinearLayoutManager.

Put this code where you finish populating your arrays:

Code

 

No big thing, isn’t it? I hope you can find the similarities in both methods and replace your existing ListView with RecyclerView.

The disadvantage of RecyclerView is that creating an onclicklistener is complicated and adding a header and footer (especially a loadmore view) is even more complex. I will do another tutorial on that as soon as I find out how to do those.

Need more downloads? Select one of my app promotion services and I will promote your app to 30 quality app review sites and millions of people on social websites.

Balint Farago

Entrepreneur, startup enthusiast, gadget fan. I travel a lot and in the meantime I develop and promote mobile apps.

  • Michael DiCioccio

    Thank you so much

www.scriptsell.net