Building code base for loading more & refresh lists with jetpack paging lib

Tram Ho

Preamble

Loading data lists with more load and refreshing is a very common task in the android dev process, today I introduce you how to do it with jetpack paging lib and create bases to reuse for different screens. .

Proceed to the code base

PagedListAdapter

To paging lib can detect the load more then we need to use PagedListAdapter , you can create an adapter or extend from BasePagedListAdapter below.

Note: paging lib uses getItem() function to detect the load more so you need to call this function in onBindViewHolder() , if you use another way to get item, for example adapter.currentList.getPosition , paging lib will Could not detect loading more list.

https://github.com/dangquanuet/The-Movie-DB-Kotlin/blob/develop/app/src/main/java/com/example/moviedb/ui/base/BaseListAdapter.kt#L80

BasePageKeyedDataSource

Paging lib currently supports 3 different types of data srouce, but PageKeyedDataSource is the most commonly used type so I will make an example of this type, the other 2 types you can do the same.

Currently PageKeyedDataSource has 3 functions loadInitial() , loadBefore() , loadAfter() to define the load status according to the page, but in fact I see that we only need a load function, so I have shortened the code. and returns a unique abstract loadDataSource() function.

Note: As far as I know, paging 3 (currently in preview) has combined the above functions into 1, really very reasonable. I will introduce it to you after it is more stable.

https://github.com/dangquanuet/The-Movie-DB-Kotlin/blob/develop/app/src/main/java/com/example/moviedb/ui/base/BasePagedRefreshViewModel.kt#L123

BasePagedRefreshViewModel

After having the data source, we will define the viewmodel, the viewmodel will have the basic logic of loading more, refreshing, PageKeyedDataSource parameters for PageKeyedDataSource and a loadData() function to get data.

https://github.com/dangquanuet/The-Movie-DB-Kotlin/blob/develop/app/src/main/java/com/example/moviedb/ui/base/BasePagedRefreshViewModel.kt#L14

BasePagedRefreshFragment

The base code fragment for the monitor uses the BasePagedRefreshViewModel so we can reuse it for different screens that have the same mechanism.

https://github.com/dangquanuet/The-Movie-DB-Kotlin/blob/develop/app/src/main/java/com/example/moviedb/ui/base/BasePagedRefreshFragment.kt#L11

Use

PagedMovieAdapter

https://github.com/dangquanuet/The-Movie-DB-Kotlin/blob/develop/app/src/main/java/com/example/moviedb/ui/screen/paged/PagedMovieAdapter.kt#L10

PagedMovieFragment

https://github.com/dangquanuet/The-Movie-DB-Kotlin/blob/develop/app/src/main/java/com/example/moviedb/ui/screen/paged/PagedMovieFragment.kt#L13

PagedMovieViewModel

https://github.com/dangquanuet/The-Movie-DB-Kotlin/blob/develop/app/src/main/java/com/example/moviedb/ui/screen/paged/PagedMovieViewModel.kt

Conclusion

You see, with the use of base code, now making screens with more refresh loads become much faster and more convenient, we can easily use for different screens with similar structure.

You can refer to the full source code at ddaay: https://github.com/dangquanuet/The-Movie-DB-Kotlin

See you in the next post .

Share the news now

Source : Viblo