【发布时间】:2018-07-28 17:04:01
【问题描述】:
我制作了一个带有入职流程的应用。 我的 MainActivity 类中有一个 recyclerview,我想在 onResume 命中时更新它(就在入职流程之后)。 所以这就是回收站 xml 的样子:
<android.support.v7.widget.RecyclerView
android:id="@+id/notifyRecycler"
android:layout_width="0dp"
android:layout_height="230dp"
android:clipToPadding="false"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
这是单元格布局:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/cardDetails"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</...>
我用 clear 和 add 交换适配器内的数据,当我将单元格布局视图高度从 0dp 更改为匹配父级时,一切正常。 它也适用于从 oncreate 首次运行,但是当我尝试从 onResume 更新时,它只是不会通知数据集已更改。 这是从 onResume 函数调用的适配器的代码:
public void setNewData(ArrayList<Model> models) {
this.notifications.clear();
this.notifications.addAll(models);
notifyDataSetChanged();
}
在 onCreate 中:
adapter = new NotificationsAdapter(new ArrayList<>(), this);
notifyRecycler.setAdapter(adapter);
在 onResume 中,我制作了 boolan var 以使其仅在恢复应用时更新:
ArrayList<NotificationModel> notificationsList = new ArrayList<>();
notificationsList.add(...);
notificationsList.add(...);
adapter.setNewData(notificationsList);
【问题讨论】:
-
你能分享一下 onCreate 和 onResume 的代码吗?您是如何认识到 onResume 没有通知数据集更改的,您是否从 onResume 传递了与 onCreate 不同的数据集?
-
是的,它只是一个硬编码数据集并将其传递给 setNewData 方法
-
如果您从方法 onCreate 和 onResume 传递相同的数据集,那么您无法注意到数据集是否更改通知第二次调用。请提供 onCreate 和 onResume 方法调用以及您传递给 setNewData 方法的数据集。
-
更新了帖子
-
根据 onCreate 和 onResume 代码,听起来应该没有任何问题。告诉我你到底遇到了什么问题? .通过查看代码,我可以看到您在通知列表中设置的任何项目,这些项目应该可以正确显示。
标签: android android-recyclerview