簡體   English   中英

Jetpack 撰寫分頁列表 UI 更新項目 Click

[英]Jetpack compose paginated list UI updation on item Click

如何使用 jetpack compose 在分頁列表中進行 UI 更改。

用例

我有一個分頁列表,其中包含數據名稱(字符串)和喜歡(布爾值)。 如果我單擊列表中的特定項目,我需要在 UI 中放置一個贊按鈕。 但是圖像不會根據條件在 UI 中更新。

片段

userList -> LazyPagingItems<AllDoctorsResponse.Data.Doctor>

//視圖模型

userList.itemSnapshotList.find { it?.id == user.id }?.liked = true

// 可組合

items(userList.itemCount){ index ->
 userList[index]?.let {
       if (it.liked == true) {
           UserCardWithLike(it, onClick = { userId ->
             onUserCardClicked(userId)
             }, onLikeChange = { isLiked, user ->
             onLikeChange(isLiked, user)
          })
       } else {
           UserCard(it, onClick = { userId ->
             onUserCardClicked(userId)
             }, onLikeChange = { isLiked, user ->
             onLikeChange(isLiked, user)
          })
          }
      }
    }

我不再使用 paging3,所以我沒有測試這段代碼,但我認為它會起作用:

items(userList.itemCount){ index ->
    userList[index]?.let {
        var liked by rememberSaveable { mutableStateOf(it.liked) }
        if (liked == true) {
            UserCardWithLike(
                it,
                onClick = { userId -> onUserCardClicked(userId) },
                onLikeChange = { isLiked, user -> liked = false }
            )
        } else {
            UserCard(
                it,
                onClick = { userId -> onUserCardClicked(userId) },
                onLikeChange = { isLiked, user -> liked = true }
            )
        }
    }
}

如果你想要 notifyItemChange 之類的東西,在 Paging3 中是不可能的。 在這種情況下,我建議嘗試重寫分頁庫,這非常容易。 https://gist.github.com/FishHawk/6e4706646401bea20242bdfad5d86a9e

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM