- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了一个有趣的情况,我的适配器有 2 种 View 类型 - HEADER
和 ITEM
。 header View 始终位于位置 0。更新数据时,我使用 DiffUtil
获取数据的差异。为了分派(dispatch)更新和重新绑定(bind),我使用了 ListUpdateCallback
:
private void dispatchUpdates(
@NonNull final RecyclerView.Adapter adapter,
@NonNull DiffUtil.DiffResult result) {
result.dispatchUpdatesTo(new ListUpdateCallback() {
@Override
public void onInserted(int position, int count) {
adapter.notifyItemRangeInserted(position + getItemOffset(), count);
}
....
因此我可以将偏移量(即始终存在的 header 的 1 个原因)添加到由 DiffUtil
计算的位置以更新正确的元素。这很好用。
当我还需要更新 header 时,就会出现问题。我手动计算 header 差异并通知适配器。与 DiffUtil
一起,代码如下所示:
DiffUtil.DiffResult diffResult = DiffUtil
.calculateDiff(new ItemDiffUtil(getItems(), newData));
Bundle headerDiff = getDiffPayload(newData);
dispatchUpdates(this, diffResult);
if (!isNullOrEmpty(headerDiff.keySet())) {
notifyItemChanged(0, headerDiff);
}
这就是拒玩好听。将多个更新分派(dispatch)给适配器(例如,插入 3 个项目),它变得困惑,我可以在我的网格中看到重复的项目(只是通过 Picasso 加载的缩略图)。如果我注释掉 notifyItemChanged(0, headerDiff);
header 显然没有更新,但其他一切正常。
我的问题是是否有可能以更有意义的方式对这些更新进行排队,以便我仍然可以使用 DiffUtil
但我的 header 也会更新?
编辑:有趣的是,如果我使用 AirBnb 的 Epoxy
而不是我自己的 RecyclerView
适配器
最佳答案
我仍然不确定为什么会这样,但我认为罪魁祸首是 Picasso
。我注意到日志中的一些内容:
当问题出现时,Picasso
永远不会完成请求:
D/Picasso: Main created [R14655] Request{content://com.app.myapp.contentprovider/thumb/20170705173259427 resize(534,534) centerCrop}
D/Picasso: Dispatcher enqueued [R14655]+0ms
D/Picasso: Hunter executing [R14655]+0ms
D/Picasso: Hunter removed [R14655]+4ms from
D/Picasso: Dispatcher canceled [R14655]+4ms
Picasso
也发生了一些奇怪的事情,因为它进入了一个无限循环,为我的网格中的每个元素创建请求:
D/Picasso: Main created [R14424] Request{content://com.app.myapp.contentprovider/thumb/20170705173054794 resize(534,534) centerCrop}
D/Picasso: Main completed [R14424] from MEMORY
<...> // repeated for each ViewHolder forever in an endless loop
无论如何,转到Glide据我所知已经解决了这个问题。
关于android - RecyclerView 在手动和通过 DiffUtil 通知更改时会感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44905859/
我创建了一个分支来开发新功能。由于这个新功能完全是作为一个新项目开发的,唯一可能的冲突来源是解决方案文件。 随着功能的开发,主分支更新了几次。当我完成开发和测试时,我做了: git checkout
我是一名优秀的程序员,十分优秀!