- While Using Pagination if data is loading then user will think that it list end here or ui stops scrolling in that moment if we want to show loading then we can user
LoadStateAdapter()
adapter
- adapter
onBindViewHolder() method have loadstate argument that will hold if view is in loading state or not so by using that it will apply loading when needed.
package com.example.paging3withroomdemo.paging.adapter
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.paging3withroomdemo.databinding.LoaderItemBinding
class LoaderAdpater : LoadStateAdapter<LoaderAdpater.LoaderViewHolder>() {
class LoaderViewHolder(private val binding: LoaderItemBinding) :
RecyclerView.ViewHolder(binding.root) {
fun onBind(loadState: LoadState) {
binding.progressBar.isVisible = loadState is LoadState.Loading
}
}
override fun onBindViewHolder(holder: LoaderViewHolder, loadState: LoadState) {
holder.onBind(loadState)
}
override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): LoaderViewHolder {
return LoaderViewHolder(
LoaderItemBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
}
}
adapter = quoteAdapter.withLoadStateHeaderAndFooter(
header = LoaderAdpater(),
footer = LoaderAdpater()
)
// or Only Footer Loading
adapter = quoteAdapter.withLoadStateFooter(
footer = LoaderAdpater(),
)
// or for only Header Loading
adapter = quoteAdapter.withLoadStateHeader(
header = LoaderAdpater(),
)