Offline first Android App with MVVM, Dagger2, RxJava, LiveData and Room — Part 5

Photo by Giammarco Boscaro on Unsplash

Part 1 → Setting up Dagger 2
Part 2 → Setting up Room
Part 3 → Setting up Retrofit
Part4 → Setting up Repository and ViewModel
Part 5 → Setting up the list with “infinite” scrolling

This part of the series is pretty interesting because I’m tackling a good feature of the app: show the list of bitcoins taken from CoinmarketCap’s API and listing them in the app. The most important thing here is that the list can have hundreds of cryptocurrencies, then the idea is not to query all in bulk but as mentioned in previous articles, after getting the response from the the API we store it in local database and query them in blocks, in a common behaviour such as lazy loading or infinite scrolling.

How to start?

Firstly, in order to allow the behaviour of infinite scrolling is important to change the Dao class to update the query as well as the firm of the function to receive both the limit (number of records to get from the query) and the offset (starting in which record) parameters and in this way, get the info in chunks of data rather than in bulk:

The above change also affects the Repository class, then getCryptocurrencies() function also changes to receive two params: limit and offset

As we can observe, the logic within the function has evolved as well in order to verify if there is an Internet connection active to get the results from the API during the call in addition to the DB, otherwise to get the results only from the DB. To support this, a new class (Utils) was created with the logic to verify the current Internet connection:

Remember to update the Android Manifest file to support “ACCESS_NETWORK_STATE” to do the connection verification.

Also a little change in Dagger2’s AppModule class to provide this new Utils class must be done:

Diving into the logic of the list

At this point, all the logic in the bottom layers of the app can support infinite scrolling, now we need to show the list. The first step is to create the layout of a list item. To keep the things in the easiest way, we are going to have a card view with only a field to show the name of the cryptocurrency:

The second step is to create the Adapter class and the View Holder. Nothing different that the usual process we use when working with lists. As mentioned before, I’m only binding the id of the cryptocurrency, in order to focus on the features rather than the visual stuff, although in the future this can change :)

One important thing to highlight here is addCryptocurrencies() function. This is key when working with the infinite scroll and with this I’m telling the adapter that a new range of items needs to be inserted in the current list, starting in one specific position (the previous list size) and finishing in the new list size.

The third step is to update the layout of the cryptocurrencies list, so a RecyclerView and a ProgressBar have been included:

How to implement infinite scrolling?

In order to allow the infinite scrolling on a RecyclerView, an implementation of the addOnScrollListener() is required. In this way, a new class named InfiniteScrollListener has been created to define this behaviour. The most important thing here is to take into consideration the use of Higher-Order Functions (A higher-order function is a function that takes functions as parameters, or returns a function.)

Taking advantage of this concept we can create our infinite scroll behaviour: receiving a function parameter that will be invoked every time before we reach the end of the list, then we can set a threshold that will make the function to be invoked a few items before and in this way we can provide a better experience and not making the user to see all the time the loader that more cryptocurrencies were requested. (for a better and detailed explanation in this article by Juan Saravia)

… and now our Activity class has also been updated in order to set the listener to the RecyclerView and with the function to be set as a parameter of the listener.

Take into consideration this: to keep a smooth scrolling experience we need to tell to the recycler view that scroll to a specific position in order to avoid unwanted movements with this line:

recycler.scrollToPosition(position — Constants.LIST_SCROLLING)

Constants class is just a class of constant values to be used within the app

Now, clean the project, rebuild it and it’s done, this branch shows the list of cryptocurrencies with infinite scrolling featured on :)

You can get the code from Github:




Android & Flutter Developer. GDE for Android & Mobile Engineer at La Haus.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Top Facts About Bitcoin On Its 11th Anniversary.

FOMO and FUD. How they work, and how to minimize their effects.


Effects of geopolitical risks on Bitcoin & US equities returns

An inside look at the moral and technical considerations of crypto social media.

Terra Collapse Could Spell End for Algorithmic Stablecoins - Blockworks

Why do we need a new format of IDO platforms and ZkPad as a new stage of launchpad evolution?

STO: Another Brightly New Wave of Blockchain

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Carlos Daniel

Carlos Daniel

Android & Flutter Developer. GDE for Android & Mobile Engineer at La Haus.

More from Medium

How to Display item names on spinner but select IDs only.

Kickstarting Ads Kit by integrating Interstitial Ads in Application (Kotlin)

How to record a script for a mobile app for JMeter

Android Runtime Permissions — Asking for multiple permissions