Problem Description:
LoadStateAdapter onBindViewHolder(holder: LoadStateViewHolder, loadState: The LoadState LoadState) indicate the loading, the error, no more, but with no more found LoadState. EndOfPaginationReached has always been a false, LoadState can only be loadState.Loading and loadState.Error
View source code:
PagingDataAdapter.kt
fun withLoadStateFooter(
footer: LoadStateAdapter<*>
): MergeAdapter {
addLoadStateListener { loadStates ->
footer.loadState = loadStates.append
}
return MergeAdapter(this, footer)
}
Copy the code
LoadStateAdapter.kt
var loadState: LoadState = LoadState.NotLoading(endOfPaginationReached = false) set(loadState) { if (field ! = loadState) { val oldItem = displayLoadStateAsItem(field) val newItem = displayLoadStateAsItem(loadState) if (oldItem &&! newItem) { notifyItemRemoved(0) } else if (newItem && ! oldItem) { notifyItemInserted(0) } else if (oldItem && newItem) { notifyItemChanged(0) } field = loadState } } /** * Returns true if the LoadState should be displayed as a list item when active. * * By default, [LoadState.Loading] and [LoadState.Error] present as list items, others do not. */ open fun displayLoadStateAsItem(loadState: LoadState): Boolean { return loadState is LoadState.Loading || loadState is LoadState.Error }Copy the code
LoadState: loadState.Loading: loadState.Error: loadState: loadState. Therefore, LoadStateAdapter has only loading and error states.
Solution:
Listening PagingDataAdapter addLoadStateListener knowable, loading list what ministries triggered loadState. Append the NotLoading (endOfPaginationReached = true).
Above, want to loadState. EndOfPaginationReached attribute to determine whether the bottom and only get loadState NotLoading state, through rewriting displayLoadStateAsItem method returns true.
override fun displayLoadStateAsItem(loadState: LoadState): Boolean {
return true
// return loadState is LoadState.Loading || loadState is LoadState.Error || loadState is LoadState.NotLoading
}
Copy the code
The loading, error, and notLoading states of loadState are all triggered.