Simplest and fast Android RecyclerView pagination
To get a Git project into your build:
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.Tobibur:pagination-android:Tag'
}
- Implement your activity or fragment with
PageListener
interface.
class MainActivity : AppCompatActivity(), PageListener {
override fun onPagination(page: Int) {
//page updates will be here
}
}
- initialize your recyclerview with
PaginationUtils
object withinitPagination
funtion
PaginationUtils.initPagination(mRecyclerView, linearLayoutManager,
this@MainActivity)
- That's it! Now just make api call in
onPagination
for pagination and add data from the reponse to the recyclerview adapter.
override fun onPagination(page: Int) {
getRepositories(page)
Log.d(TAG, "onPagination: $page")
}
// Network call using coroutine example
private fun getRepositories(page: Int = 1) {
CoroutineScope(Dispatchers.IO).launch {
try {
val repos = mService.getRepos("Android", page)
withContext(Dispatchers.Main){
//added new data in the adapter
recyclerViewAdapter.addAll(repos.items)
}
}catch (e:Exception){
Log.d(TAG, "getRepositories: ${e.message}")
}
}
}
Please refer to the sample app for full example source code.
Licensed under the MIT License.