gpt4 book ai didi

android - 第 3 页初始加载未显示

转载 作者:行者123 更新时间:2023-12-04 11:51:49 24 4
gpt4 key购买 nike

我正在使用第 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)
}
}
负载状态 View 支架
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)
Activity
    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返回 ConcatAdapter连接原始 PagingDataAdapter 的结果与 LoadStateAdapterCombinedLoadState.append事件。因此,预计它不会在初始加载期间返回项目(loadType == 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)

关于android - 第 3 页初始加载未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63418745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com