1

So, i'm trying to follow the paging library. and in most examples, they have something like:

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

Why do you have to check for the item in the adapter being null? I'm not understanding the internals of the PagedListAdapter flow. Could anyone please explain?

My guess is that we have an observer on the adapter that "nukes" the adapter's content from the UI thread at some point, as soon as the datasource is updated, and thus this item position is outdated?

David T.
  • 22,301
  • 23
  • 71
  • 123

2 Answers2

3

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/

LonelyCpp
  • 2,533
  • 1
  • 17
  • 36
2

It's explained in the official docs:

@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
Alessio
  • 3,063
  • 1
  • 15
  • 17
  • 2
    Where does this clear method come from? I search this method in view holder implementations. Is that a custom method which we have to create inside the view holder or a predefined one? – Guru Karthi R Jan 23 '19 at 07:17
  • 1
    @GuruKarthiR it's a method you've to define to clear the placeholder item. As you can see, holder is a UserViewHolder reference, and UserAdapter extends PagedListAdapter, whereas UserViewHolder is defined as VH extends android.support.v7.widget.RecyclerView.ViewHolder, which - as you said - doesn't define any clear() method. – Alessio Jan 24 '19 at 06:37
  • What should the clear() method will have? Any clue? do I need to reset the data members back to the default or something? @aliessio – Guru Karthi R Jan 24 '19 at 07:05
  • 2
    @GuruKarthiR that depends on your VH, right? For instance say your VH contains 2 TextViews, you need to clean up their texts (say tv.setText("")). Read about null placeholders [here](https://developer.android.com/reference/android/arch/paging/PagedList). Basically you load the data in chuncks, and a null placeholder (by position) means that data at that position is not available yet, but it will be later on in future, and still you can show the memory allocated UI view through the screen, but definitely not with any old data which will pollute your list. Does it clarify / help? – Alessio Jan 24 '19 at 07:44
  • @GuruKarthiR remember that the views are and can be recycled at runtime, as we're in the context of a RecyclerView. – Alessio Jan 24 '19 at 07:47