tags:

views:

182

answers:

2

I'm trying to implement paging in a custom ListAdapter. Right now I'm just making the request for the next page when the last item in the ListView becomes visible, by checking in getView() if position is >= the size of ListAdapter.getCount().

It works fine, but I'm wondering if there's a better way (or a different way) that will only make the request once the last item in the list is actually visible to the user. Anyone know of a way?

A: 

Try removing the check altogether. In my experience, getView() is only called when the entry is about to come on screen.

Jim Blackler
Not sure what you mean. The check has to be there, else it would page every row?
synic
@Jim Blackler: Pls describe it more.
OneWorld
+1  A: 

I'm doing it almost the same way:

public static final int SCROLLING_OFFSET = 5;
// ...
private final ArrayList<T> items = new ArrayList<T>();
// ...
if (SCROLLING_OFFSET == items.size() - position) {
    if (hasNextPage()) {
        addNextPage();
    }
}

private boolean hasNextPage() {
    // basically calculates whether the last 2 pages contained the same # of items
}

private void addNextPage() {
    // show spinner
    // fetch next page in a background thread
    // add to items
    notifyDataSetChanged();
}
alex