Skip to main content

How to implement pagination in a RecyclerView in Kotlin Android

How to implement pagination in a RecyclerView in Kotlin Android.

Here's a step-by-step tutorial on how to implement pagination in a RecyclerView in Kotlin for Android.

Prerequisites

Before we get started, make sure you have the following set up:

  • Android Studio installed on your system
  • Basic knowledge of Kotlin and Android development
  • A project set up with a RecyclerView in your layout file

Step 1: Add dependencies

Open your project's build.gradle file and make sure you have the following dependencies added:

implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.paging:paging-runtime-ktx:3.0.0'

Sync your project to download the required dependencies.

Step 2: Create data source

Create a class that will act as the data source for your RecyclerView. This class should extend PagingSource and override the load function. Here's an example:

class MyDataSource(private val apiService: ApiService) : PagingSource<Int, MyData>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, MyData> {
try {
val nextPage = params.key ?: 1
val response = apiService.getData(nextPage)
val data = response.data
val prevKey = if (nextPage == 1) null else nextPage - 1
val nextKey = if (data.isEmpty()) null else nextPage + 1

return LoadResult.Page(
data = data,
prevKey = prevKey,
nextKey = nextKey
)
} catch (e: Exception) {
return LoadResult.Error(e)
}
}
}

In this example, we assume that MyData is the data model class for your items, and ApiService is your API service class that fetches the data.

Step 3: Create a PagingSource factory

Create a factory class that will create instances of your data source. This class should implement PagingSource.Factory and override the create function. Here's an example:

class MyDataSourceFactory(private val apiService: ApiService) : PagingSource.Factory<Int, MyData>() {
override fun create(): PagingSource<Int, MyData> {
return MyDataSource(apiService)
}
}

Step 4: Create a PagingConfig

Create a PagingConfig object that defines the configuration for your pagination. This object allows you to set various parameters like page size, prefetch distance, and initial load size. Here's an example:

val pagingConfig = PagingConfig(
pageSize = 20,
prefetchDistance = 5,
initialLoadSize = 20
)

Step 5: Create a Pager

Create a Pager object that will handle the pagination logic. Pass the PagingConfig and your PagingSource.Factory to the Pager constructor. Here's an example:

val pager = Pager(
config = pagingConfig,
pagingSourceFactory = { MyDataSourceFactory(apiService) }
)

Step 6: Observe the PagingData

Observe the PagingData returned by the Pager using the Flow.collectLatest function. This will give you a stream of paged data that you can submit to your RecyclerView's adapter. Here's an example:

lifecycleScope.launch {
pager.flow.collectLatest { pagingData ->
adapter.submitData(pagingData)
}
}

In this example, adapter is the adapter for your RecyclerView.

Step 7: Update the adapter

Update your RecyclerView's adapter to work with PagingData. You can use the PagingDataAdapter class provided by the Paging library, which simplifies the process of updating the adapter with paged data. Here's an example:

class MyAdapter : PagingDataAdapter<MyData, MyViewHolder>(MyDataComparator) {
// Implement onCreateViewHolder and onBindViewHolder as usual

object MyDataComparator : DiffUtil.ItemCallback<MyData>() {
override fun areItemsTheSame(oldItem: MyData, newItem: MyData): Boolean {
return oldItem.id == newItem.id
}

override fun areContentsTheSame(oldItem: MyData, newItem: MyData): Boolean {
return oldItem == newItem
}
}
}

In this example, MyViewHolder is the ViewHolder class for your items, and MyDataComparator is a custom DiffUtil.ItemCallback that determines if items are the same or have the same contents.

Step 8: Handle item clicks

If you want to handle item clicks in your RecyclerView, you can add an OnClickListener to your ViewHolder class. Here's an example:

class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
init {
itemView.setOnClickListener {
val position = bindingAdapterPosition
if (position != RecyclerView.NO_POSITION) {
val item = getItem(position)
// Handle item click
}
}
}
}

In this example, getItem is a function provided by the PagingDataAdapter to get the item at a specific position.

That's it! You have now implemented pagination in a RecyclerView using the Paging library in Kotlin for Android.