RecyclerView 选择从 LiveData 观察加载的项目
我的 ViewModel 中有一个 LiveData,它是从 Room 数据库加载的,经过转换后,我将其显示在 RecyclerView 中。我想要的是具有选择功能。
ShareViewModel 类
val serverConnections: LiveData<List<ShareConnectionModelUi>> =
Transformations.map(shareManager.serverWithAccounts) {
// Here **it**, which is a Map<Server, List<Account>> gets transformed into a list
// of ShareConnectionModelUi
// e.g. Map of
// Server1, [Account1, Account11]
// Server2, [Account2, Account22]
// will be transformed to
// [Server1, Account1, Account11, Server2, Account2, Account22 ...]
}
// The UiModel
sealed class ShareConnectionModelUi {
class ServerItem(val server: Server): ShareConnectionModelUi()
class ConnectionItem(val connectionItem: Account, var checked: Boolean): ShareConnectionModelUi()
}
在 Fragment 中,我观察 LiveData 并将其传递给 RecyclerAdapter,后者通过 ViewType 呈现相对布局。因此,当布局为 ShareConnectionModelUi.ConnectionItem 时,我希望用户能够通过单击来选中或取消选中该项目。我使用 ConnectionItem 布局的接口。但是,我如何更新 LiveData 以便将 checkImage 显示到 ConnectionItemLayout,因为 checkImg 的可见性是通过 ShareConnectionModelUI.ConnectionItem.check 实现的?
我想我必须更新 LiveData。这是Viewmodel内部的试用功能,它不起作用......
fun selectConnection(account: Account) {
serverConnections.value?.let {
it.find { s ->
s is ShareConnectionModelUi.ConnectionItem
&& s.connectionItem.serverId == account.serverId
&& s.connectionItem.accountId == account.accountId
}?.let { s ->
(s as ShareConnectionModelUi.ConnectionItem).checked == checked
}
}
}
I have a LiveData from my ViewModel that are loaded from the Room Database and after a transformation i display it in a RecyclerView. What i want is to have the select functionality.
ShareViewModel class
val serverConnections: LiveData<List<ShareConnectionModelUi>> =
Transformations.map(shareManager.serverWithAccounts) {
// Here **it**, which is a Map<Server, List<Account>> gets transformed into a list
// of ShareConnectionModelUi
// e.g. Map of
// Server1, [Account1, Account11]
// Server2, [Account2, Account22]
// will be transformed to
// [Server1, Account1, Account11, Server2, Account2, Account22 ...]
}
// The UiModel
sealed class ShareConnectionModelUi {
class ServerItem(val server: Server): ShareConnectionModelUi()
class ConnectionItem(val connectionItem: Account, var checked: Boolean): ShareConnectionModelUi()
}
In the Fragment i observe the LiveData and pass it to the RecyclerAdapter, who by the ViewType presents the relative layout. So when the Layout is ShareConnectionModelUi.ConnectionItem, i want the user to be able to check or uncheck the item by clicking on it. I use an interface to the ConnectionItem layout. However, how can i update the LiveData in order to display the checkImage to the ConnectionItemLayout since the visibility of the checkImg is through the ShareConnectionModelUI.ConnectionItem.check??
I guess that i have to update the LiveData. Here is a trial function inside the Viewmodel, which did not work...
fun selectConnection(account: Account) {
serverConnections.value?.let {
it.find { s ->
s is ShareConnectionModelUi.ConnectionItem
&& s.connectionItem.serverId == account.serverId
&& s.connectionItem.accountId == account.accountId
}?.let { s ->
(s as ShareConnectionModelUi.ConnectionItem).checked == checked
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此比较的结果未使用:
这应该是一个赋值吗?
The result of this comparison is unused:
Should this be an assignment instead?