1

所以,我正在尝试关注分页库。在大多数示例中,它们具有以下内容:

@Override
public void onBindViewHolder(@NonNull PokemonViewHolder pokemonViewHolder, int i) {
    Pokemon pokemon = getItem(i); 
    if (pokemon != null) { // <-- why this check here?
        pokemonViewHolder.bind(pokemon);
    }
}

为什么必须检查适配器中的项目是否为空?我不了解 PagedListAdapter 流的内部结构。谁能解释一下?

我的猜测是,一旦数据源更新,我们在适配器上有一个观察者,它会在某个时刻从 UI 线程“核对”适配器的内容,因此这个项目位置已经过时了?

4

2 回答 2

2

在官方文档中有解释:

@Override
public void onBindViewHolder(UserViewHolder holder, int position) {
    User user = getItem(position);
    if (user != null) {
        holder.bindTo(user);
    } else {
        // Null defines a placeholder item - PagedListAdapter will automatically invalidate
        // this row when the actual object is loaded from the database
        holder.clear();
    }
}
  1. https://developer.android.com/reference/android/arch/paging/PagedListAdapter
于 2018-10-26T09:00:49.090 回答
2

始终具有数据集的PagedList完整大小。默认情况下, 将为尚未加载PagedList的数据返回 null 。

这意味着在我们的适配器中,我们确实需要记住在我们的绑定方法中检查 null。

http://blog.abnormallydriven.com/2017/09/30/introducing-the-paging-library/

于 2018-10-26T09:05:34.610 回答