How to update ListView on scrolling while retrievi

2019-03-08 22:12发布

Currently, I'm using AsyncTask to handle Http connection and retrieve data as JSON format.
Loading all data is trivial but it consumes too much time, so I decided to switch to load 10 items at a time using LIMIT OFFSET (mysql).
Next I set up the event onScroll for my list view to create a new AsyncTask each time user scroll. However, from what I read, AsyncTask is stored in a thread pool which is limited 5 threads at a time, so I'm not sure this is a right approach. I'm newbie to client/server app, so could I anyone give me an advice on this issue? Any related article, documentation would be greatly appreciated.

3条回答
Melony?
2楼-- · 2019-03-08 22:30

A typical approach would be e.g. to load 25 initially and then have a footer in the list that displays e.g. the current count and the total count and upon pressing loads another 25 and so on. That would be a paged sort of loading.

When you do that you have to keep the current position and notify the adapter that the list has changed.

查看更多
男人必须洒脱
3楼-- · 2019-03-08 22:37

If you are using a ListView, I believe I can safely assume that you must be using some sort of ListAdapter. Instead of starting a new AsyncTask in the onScroll event, you should maintain just one single AsyncTask to retrieve data from the server, add that data to the ListAdapter dataset and then call notifyDatasetChanged on the ListAdapter.

The ListAdapter and ListView will take care of the rest.

查看更多
一纸荒年 Trace。
4楼-- · 2019-03-08 22:48

Here are few useful links for it,

Android: Implementing progressbar and "loading..." for Endless List like Android Market

Endless Listview with current Async Task

Android Endless List

http://www.androidguys.com/2009/10/21/tutorial-autogrowing-listview/

http://mylifewithandroid.blogspot.com/2010/03/progressively-loading-listviews.html

In simple steps,

As user scrolls – detect the end of the list
1)Display a progress notification
2)Ask for update
3)Receive update (asynchronously) and extend list

查看更多
登录 后发表回答