gpt4 book ai didi

android - 如何在 onResume() 上更新 RecyclerView 中的值?

转载 作者:太空宇宙 更新时间:2023-11-03 13:11:16 25 4
gpt4 key购买 nike

我正在设计一个新闻应用程序,在其中我在 recyclerView 中显示新闻文章。现在,在单击新闻文章时,我想更改其背景颜色以指示已阅读该新闻项目。

为此,我首先在阅读新闻时更新与我的 Firebase 数据库中的新闻文章相对应的状态字段。然后我在我的回收器适配器中检查这个字段的值,如果状态发生变化,则更改背景。

然而,由于 recyclerView 的适配器是在 fragment 的 onCreateView 中定义的,所以当我按下后退按钮时,更改不会立即发生。相反,更改会在重新打开应用程序时发生,因为此时调用了 onCreateView。那么如何在 fragment 的 onResume 中更新 adapter 并相应地更新 recyclerView 呢?

最佳答案

覆盖onResume方法并调用notifyDataSetChanged:

@Override
public void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}

在 Kotlin 中:

override fun onResume() {
super.onResume()
adapter.notifyDataSetChanged()
}

关于android - 如何在 onResume() 上更新 RecyclerView 中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44601422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com