我正在设计一个新闻应用程序,在其中我在 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/