【问题标题】:onBindViewHolder not called after calling diffResult.dispatchUpdatesTo(this)调用 diffResult.dispatchUpdatesTo(this) 后未调用 onBindViewHolder
【发布时间】:2025-11-26 05:55:02
【问题描述】:

我正在将我的数据设置为回收站视图:

public void setData(List<Notification> newNotifications) {
        DiffUtil.DiffResult diffResult = getDiffBetweenNewAndOldList(newNotifications);
        diffResult.dispatchUpdatesTo(this);
        this.notificationList.clear();
        this.notificationList.addAll(newNotifications);
    }

现在我面临的问题是,当我获取新的附加数据时(例如,旧列表的大小为 10,而我从下一页获取下 10 个值的数据),当时我的 onBindViewHolder 被调用:

@Override
    public void onBindViewHolder(@NonNull NotificationViewHolder holder, int position) {
        holder.bindView(notificationList.get(position));
        if (position == notificationList.size() - 1) {
            EventBus.getDefault().post(new UpdateEvent.GetNotificationList(GetNotificationDataFrom.ON_LOAD_NEXT_PAGE, notificationList.get(position).getNotification_time()));
        }
    }

当我刷新数据并再次获取列表的第一页时,不会调用 onBindViewHolder。当我使用 notifyDataSetChanged() 时没有这样的问题。我该如何解决这个问题?

【问题讨论】:

    标签: android android-recyclerview notifydatasetchanged android-diffutils


    【解决方案1】:

    你可以试试:

    public void setData(List<Notification> newNotifications) {
            DiffUtil.DiffResult diffResult = getDiffBetweenNewAndOldList(newNotifications);
            this.notificationList.clear();
            this.notificationList.addAll(newNotifications);
            diffResult.dispatchUpdatesTo(this);
    }
    

    【讨论】:

    • 结果相同