PagingDataAdapter class 在调用 adapter.submitData 方法后未执行
PagingDataAdapter class not executed after called adapter.submitData method
我想在我的 android 应用程序中试用 Paging 3。它成功,直到我从 class PagingDataSource 获得我的 PagingData<*> 列表。但是每次我调用 adapter.submitData(it) 时,我的适配器 class 都不会执行。请帮忙,我的错误在哪里?非常感谢。
这是我要调用的代码homeFragment.kt
private lateinit var webinarListAdapter: myAdapter
fun initAdapter() {
webinarAdapter = myAdapter()
with(recyclerView) {
layoutManager = LinearLayoutManager(requireContext())
adapter = webinarAdapter
adapter = webinarAdapter.withLoadStateHeaderAndFooter(
header = WebinarLoadStateAdapter { webinarAdapter.retry() },
footer = WebinarLoadStateAdapter { webinarAdapter.retry() }
)
}
lifecycleScope.launch {
viewModel.user.collectLatest {
webinarAdapter.submitData(it)
}
}
}```
MyAdapter.kt class
```class myAdapter: PagingDataAdapter<User, UserHolder>(DIFF_CALLBACK) {
override fun onBindViewHolder(holder: UserHolder, position: Int) {
Log.e(TAG, "onBindViewHolder")
holder.onBind(getItem(position)!!)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserHolder {
return UserHolder.create(parent)
}
companion object {
private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<User>() {
override fun areItemsTheSame(oldItem: User, newItem: User) =
oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: Taklim, newItem: Taklim) = oldItem == newItem
}
}
}```
将此添加到您的适配器
fun submitList(list: List<T>) {
yourlist.submitList(list)
notifyDataSetChanged()
}
经过越来越多的调试,我知道我的错误在哪里。我的错误是在我的分页数据源上执行了捕获异常。所以我的适配器没有执行,我的视图模型没有收到任何数据。谢谢!
我想在我的 android 应用程序中试用 Paging 3。它成功,直到我从 class PagingDataSource 获得我的 PagingData<*> 列表。但是每次我调用 adapter.submitData(it) 时,我的适配器 class 都不会执行。请帮忙,我的错误在哪里?非常感谢。
这是我要调用的代码homeFragment.kt
private lateinit var webinarListAdapter: myAdapter
fun initAdapter() {
webinarAdapter = myAdapter()
with(recyclerView) {
layoutManager = LinearLayoutManager(requireContext())
adapter = webinarAdapter
adapter = webinarAdapter.withLoadStateHeaderAndFooter(
header = WebinarLoadStateAdapter { webinarAdapter.retry() },
footer = WebinarLoadStateAdapter { webinarAdapter.retry() }
)
}
lifecycleScope.launch {
viewModel.user.collectLatest {
webinarAdapter.submitData(it)
}
}
}```
MyAdapter.kt class
```class myAdapter: PagingDataAdapter<User, UserHolder>(DIFF_CALLBACK) {
override fun onBindViewHolder(holder: UserHolder, position: Int) {
Log.e(TAG, "onBindViewHolder")
holder.onBind(getItem(position)!!)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserHolder {
return UserHolder.create(parent)
}
companion object {
private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<User>() {
override fun areItemsTheSame(oldItem: User, newItem: User) =
oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: Taklim, newItem: Taklim) = oldItem == newItem
}
}
}```
将此添加到您的适配器
fun submitList(list: List<T>) {
yourlist.submitList(list)
notifyDataSetChanged()
}
经过越来越多的调试,我知道我的错误在哪里。我的错误是在我的分页数据源上执行了捕获异常。所以我的适配器没有执行,我的视图模型没有收到任何数据。谢谢!