Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView/ListAdapter paging

Tags:

android

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?

like image 485
synic Avatar asked Dec 07 '25 06:12

synic


2 Answers

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();
}
like image 59
yanchenko Avatar answered Dec 08 '25 19:12

yanchenko


I think there is a better way to do it. Implementing the OnScrollListener interface. Take a look at this: Endless Scrolling ListView

like image 27
dds Avatar answered Dec 08 '25 21:12

dds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!