Paging Library

Daniyar Nurgaliyev
5 min readJan 25, 2021

--

Photo by Jan Antonin Kolar on Unsplash

Plan:

1. Create app with RoomDatabase. Git branch
2. Migrate from RecyclerView.Adapter to ListAdapter in Adapter. Git branch
3. Add Paging Library version 2. Git branch
4. Migrate to Paging Library version 3. Git branch

1. Create app with RoomDatabase

Create Room Database according to Google’s recommended structure of app using Architecture Components.

Google’s recommended app architecture

In more depth you can read from this codelab. Here will be abstract information.

So to create app with Room Database we will have to do following:

Steps:
//root
1.1. Add dependencies

//data package
1.2.1. Create layouts: activity_main.xml and recyclerview_item.xml
1.2.2. Create Word.kt model file
1.2.3. Create WordDao.kt file
1.2.4. Create WordDatabase.kt file
1.2.5. Create WordRepository.kt file

//ui package
1.3.1. Create WordViewModel.kt file
1.3.2. Create WordViewModelFactory.kt file
1.3.3. Create WordViewHolder.kt file
1.3.4. Create WordListAdapter.kt file
1.3.5. Modify MainActivity.kt file

//root
1.4.1. Create WordsApplication.kt file
1.4.2. Modify AndroidManifest.xml file

1.1. Add dependencies

According to Google's codelab we need to add these dependencies:

Add to build.gradle(Module):

Add to build.gradle(Project):

1.2.1. Create layouts

Create recyclerview_item.xml file:

Create activity_main.xml file:

With FloatingActionButton we will be adding new item into the list.

1.2.2. Create Word.kt model file

1.2.3. Create WordDao.kt file

1.3.2. Create WordViewModelFactory.kt file

More about ViewModelFactory you can read here

App launch

If we launch the application, we can see instant loaded list from database to recyclerView:

2. Migrate from RecyclerView.Adapter to ListAdapter in Adapter

2.1. Change WordListAdapter.kt

We have to do these changes:

So the WordListAdapter.kt would look like this:

2.2. Change MainActivity.kt

We have to do following changes:

So the list that we will have to pass to adapter would be called using submitList(…) function:

App launch

If we launch the application, nothing will change from previous launch. It will still load all data at once from database to recyclerView:

3. Add Paging Library version 2

3.1. Add dependency
3.2. Change WordDao.kt
3.3. Change WordRepository.kt
3.4. Change WordViewModel.kt
3.5. Change WordListAdapter.kt

3.1. Add dependency

In build.gradle (Module):

In build.gradle (Project):

3.2. Change WordDao.kt

We have to change return value from Flow to DataSource.Factory:

So the modified WordDao.kt would look like this:

3.3. Change WordRepository.kt

We have to convert LiveData by using toLiveData function and giving this function pageSize. So the changes will be following:

Modified WordRepository.kt would look like this:

3.4. Change WordViewModel.kt

We will have to change from List to PagedList:

Modified WordViewModel.kt would look like this:

3.5. Change WordListAdapter.kt

We have to change implementation from ListAdapter to PagedListAdapter. Then add null check for getItem(…) function in onBindViewHolder(…)

Modified WordListAdapter.kt would look like this:

App launch

As we set pagesize to 5, you can see that when the app launches it loads 5 pages at a time. When the user would scroll down the list, new data will be loaded from database.

4. Migrate to Paging Library version 3

4.1. Change dependency
4.2. Change WordDao.kt
4.3. Change WordRepository.kt
4.4. Change WordViewModel.kt
4.5. Change WordListAdapter.kt
4.6. Change MainActivity.kt

4.1. Change dependency

In build.gradle (Project):

Beside the pagingLibrary version change, also we have to change the Room Database version.

The modified build.gradle (Project):

4.2. Change WordDao.kt

We have to change return value from DataSource.Factory to PagingSource:

Modified WordDao.kt would look like this:

4.3. Change WordRepository.kt

We will be returning Flow instead of LiveData:

Modified WordRepository.kt would look like this:

4.4. Change WordViewModel.kt

We will be returning Flow instead of LiveData:

Modified WordViewModel.kt would look like this:

cachedIn(…) function was added to cache the data in list, as when the configuration changes e.g. screen rotation, data will not be queried repeatedly.

Please check the App launch part at the end of this article to visualise the meaning.

4.5. Change WordListAdapter.kt

Implementation will need to be changed from PagedListAdapter to PagingDataAdapter:

Modified WordListAdapter.kt would look like this:

4.6. Change MainActivity.kt

collecLatest(…) function need to be called from coroutine scope and we need to change list passing function to submitData(…):

Modified MainActivity.kt would look like this:

App launch

The app would still launch the same way as it did previously with pagingLibrary version 2 by grabbing portions of data from database as the user scrolls down the list.

Let's in WordRepository.kt file change the pagesize to 1 and see how the pages are being loaded:

Again in WordRepository.kt file, now let's keep pagesize 1, but change placeholder to true:

In WordViewModel.kt let's delete cachedIn(…) function and initiate configuration change e.g. rotate the screen and see (pagesize will be 1, placeholder = false):

with cachedIn(…) function:

without cachedIn(…) function:

Have Fun 😉

--

--