为什么 onBindViewHolder 中的 getItem 永远为空?

Why would getItem in onBindViewHolder ever be null?

所以,我正在尝试关注分页库。在大多数例子中,他们有类似的东西:

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

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

我的猜测是,我们在适配器上有一个观察器,一旦数据源更新,"nukes" 适配器的内容就会在某个时候从 UI 线程中获取,因此这个项目的位置过时了吗?

官方文档中有说明:

@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

The PagedList always has the full size of the dataset. The PagedList will by default return null for data that isn't loaded yet.

This means that in our adapter we do need to remember to check for null in our bind method.

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