RecyclerView 选择从 LiveData 观察加载的项目

发布于 2025-01-16 22:34:47 字数 1720 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冧九 2025-01-23 22:34:47

此比较的结果未使用:

(s as ShareConnectionModelUi.ConnectionItem).checked == checked

这应该是一个赋值吗?

(s as ShareConnectionModelUi.ConnectionItem).checked = checked

The result of this comparison is unused:

(s as ShareConnectionModelUi.ConnectionItem).checked == checked

Should this be an assignment instead?

(s as ShareConnectionModelUi.ConnectionItem).checked = checked
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文