android - 第 3 页初始加载未显示
问题描述
我正在使用第 3 页,除了初始加载状态外,一切正常。我正在添加withLoadStateFooter
,但它在第一次调用时从不显示加载状态
这是我的实现
负载状态适配器
class LoadStateAdapter (
private val retry: () -> Unit
): LoadStateAdapter<LoadStateViewHolder>() {
override fun onBindViewHolder(holder: LoadStateViewHolder, loadState: LoadState) {
holder.bindTo(loadState)
}
override fun onCreateViewHolder(
parent: ViewGroup,
loadState: LoadState
): LoadStateViewHolder {
return LoadStateViewHolder.create(parent, retry)
}
}
加载状态视图持有人
class LoadStateViewHolder(
view : View,
private val retryCallback: () -> Unit
) : RecyclerView.ViewHolder(view) {
private val progressBar = view.findViewById<ProgressBar>(R.id.progress_bar)
private val errorMsg = view.findViewById<TextView>(R.id.error_msg)
private val btnRetry = view.findViewById<Button>(R.id.retry_button)
.also {
it.setOnClickListener { retryCallback() }
}
private var loadState : LoadState? = null
companion object {
fun create(parent: ViewGroup, retryCallback: () -> Unit): LoadStateViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.network_state_item, parent, false)
return LoadStateViewHolder(
view,
retryCallback
)
}
}
fun bindTo(loadState: LoadState) {
this.loadState = loadState
btnRetry.isVisible = loadState !is LoadState.Loading
errorMsg.isVisible = loadState !is LoadState.Loading
progressBar.isVisible = loadState is LoadState.Loading
if (loadState is LoadState.Error){
errorMsg.text = loadState.error.localizedMessage
}
}
}
寻呼源
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Model> {
try {
// Load page 1 if undefined.
val currentPage = params.key ?: 0
val offset = currentPage * 50
val requestParams = hashMapOf<String, Any>()
requestParams.put("limit", 50)
requestParams.put("offset", offset)
val response = repository.getList(requestParams)
val isFinish = response.paging != null && response.paging!!.next == null
return LoadResult.Page(
data = response.data ?: mutableListOf(),
prevKey = null, // Only paging forward.
nextKey = if (isFinish) null else currentPage + 1
)
} catch (e: Exception) {
// Handle errors in this block
return LoadResult.Error(e)
}
}
查看模型
val listPagingFlow = Pager(PagingConfig(pageSize = 50)) {
MyPagingSource(repository)
}.flow.cachedIn(viewModelScope)
活动
val pagingAdapter = MyPagingAdapter()
list.apply {
setHasFixedSize(true)
adapter = pagingAdapter.withLoadStateFooter(
footer = LoadStateAdapter { pagingAdapter.retry() }
)
}
lifecycleScope.launch(Dispatchers.IO) {
viewModel.listPagingFlow.collectLatest { pagingData ->
pagingAdapter.submitData(pagingData)
}
}
MyPagingAdapter很简单PagingDataAdapter
简而言之; 加载状态工作正常,但在第一次请求时没有显示。任何人都可以帮忙吗?
当前版本3.0.0-alpha04
解决方案
withLoadStateFooter
返回将原始结果与侦听事件的 aConcatAdapter
连接起来的 a。所以在初始加载(loadType ==)期间它不会返回一个项目,它是这样设计的,因为在任何项目加载之前显示“页脚”实际上没有意义。PagingDataAdapter
LoadStateAdapter
CombinedLoadState.append
REFRESH
但是,要实现您想要的,您可以简单地创建自己的 ConcatAdapter ,它.withLoadStateFooter
非常接近地反映了以下实现:
val originalAdapter = MyPagingDataAdapter(...)
val footerLoadStateAdapter = MyFooterLoadStateAdapter(...)
addLoadStateListener { loadStates ->
// You need to implement some logic here to update LoadStateAdapter.loadState
// based on however you want to prioritize between REFRESH / APPEND load states.
// Something really basic might be:
// footerLoadStateAdapter.loadState = when {
// loadStates.refresh is NotLoading -> loadStates.append
// else -> loadStates.refresh
// }
footerLoadStateAdapter.loadState = ...
}
return ConcatAdapter(originalAdapter, footerLoadStateAdapter)
推荐阅读
- javascript - 如何从分页 JSON 中提取数据
- bash - 函数定义了两次
- jsf - 如何通过甜蜜警报重定向到另一个页面?
- c# - “消息”:“无法建立 SSL 连接,请参阅内部异常。” 在 Windows Server 2012 R2 中
- python-3.x - Boto3:根据多个标签获取自动缩放组名称
- android - 如何计算有权访问证书的正确 apkCertificateDigestSha256?
- javascript - 将对象作为属性发送到反应组件
- python-3.x - 如何解决 OperationalError:靠近“(”:sqldf python 中的语法错误?
- c# - 如何为 SonarQube 添加覆盖范围?
- react-native - 如何过滤领域中的对象?