在我的片段中
lifecycleScope.launch {
viewModel.capture().collectLatest {
adapter.submitData(it)
}
}
现在,当我更新Recyclerview
中的项目时。
repository.update(bean)
db.withTransaction {
db.dao().update(bean)
service.update(requestBody)
}
因为我将paging3与remoteMediator一起使用,比如:在道:
@Query("SELECT * FROM content ORDER BY ... ")
fun getAll(): PagingSource<Int,Content>
在视图模型中:
fun capture() = repository.captureYiMaFlow()
存储库中:
fun capture()=
Pager(
config = PagingConfig(pageSize = 6),
remoteMediator = SettingMediator(service, db)) {
db.dao().getAll()
}.flow
房间将自动更改recyclerview中的项目数据。因此数据库中的数据将被更新。因为我调用了service.update(requestBody)
,所以服务器中的数据将被更新。
但recycleview的ui不会改变。我需要关闭片段并再次打开,然后UI会发生变化。(注意,recyclerview的数据已经发生了变化。例如:recyclerview的第一个项目的数据是100
,我将其更新为1000
,然后当我点击该项目时,数据为1000
,但UI数据没有变化,它是100
…(
虽然我可以使用adapter.notifyDataSetChange()
来更新UI,但我认为还有很多其他方法可以完成
在片段中。添加adapter.notifyDataSetChanged()
,如:更改
lifecycleScope.launch {
viewModel.capture().collectLatest {
adapter.submitData(it)
}
}
至
lifecycleScope.launch {
viewModel.capture().collectLatest {
adapter.notifyDataSetChanged()
adapter.submitData(it)
}
}
上面的代码可以解决这个问题。
然而,如果我更改这两行代码的顺序,它将不会像预期的那样工作
这意味着如果我使用
lifecycleScope.launch {
viewModel.capture().collectLatest {
adapter.submitData(it)
adapter.notifyDataSetChanged()
}
}
它不会像预期的那样工作。。。。。