JustAMartin
JustAMartin

Reputation: 13753

Load new ListView items on scroll without using OnScrollListener

I have seen some ListView dynamic growing implementations which use OnScrollListener.

I would like to know, are there any more elegant solutions? Doesn't ListView itself request more data from the Adapter if Adapter says there are more items available?

After all, the ListView creates its views on demand while scrolling, so why should I create my own scroll listener? Theoretically, I should be able to respond to ListView's requests that are issued to the underlying Adapter while the ListView is being scrolled. I could issue new data requests to the server (in batches) when ListView tells its Adapter that it needs more data for the next views, and when the loading completes, I could notify the ListView that the new data is available.

Has anyone tried such approach? Is there any source code available?

I need to implement some kind of endless list, but it actually will have a limited count of JSON records (about 500), and each record will have an unique Id, so I can cache them in a simple Map. I just would like to implement it using the most elegant and straight-forward method (preferably compatible with Android 1.6) avoiding various hacky solutions.

Upvotes: 1

Views: 1404

Answers (2)

gwvatieri
gwvatieri

Reputation: 5183

As manjusg said you could use this trick in your getView method:

...
if (position > lastViewed && position == getCount()-1) {
    lastViewed = position;
    runYourTask();
}
...

Upvotes: 1

manjusg
manjusg

Reputation: 2263

After all, the ListView creates its views on demand while scrolling, so why should I create my own scroll listener?

No listview doesn't create the views. Its the adaptor that creates the views.

You can try requesting for more data in getview function by keeping track of which views position is requested by the Listview. But the elegant solution will remain with onscrolllistener.

Upvotes: 4

Related Questions