Umang
Umang

Reputation: 593

android - progressive loading of list

I am working on an app where I need to display a lot of data in a list, say 100 items. I was thinking of implementing it like facebook app does, progressively loading all the data once the user scrolls down to the bottom of list. Can anyone help me on this? Also, if this doesnt work, Can you suggest some ideas of implementing this? One thing I thought was putting the Load more option in the menu and then iteratively calling for more data.

Thanks!

Upvotes: 0

Views: 2738

Answers (2)

David Olsson
David Olsson

Reputation: 8225

You should use onScrollListener and depending on how many items that are left you load more. IE by calling the webservice with index numbers, start and end.

Here is an example I have used: In this example I always fetch the next 15 items from the server. and the first if case is that it starts fetching new items when there is only x number of items left. X is 2* the number of visisble items on the screen. It depends on the screen density.

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    if((totalItemCount-(firstVisibleItem+visibleItemCount) <= visibleItemCount*2)) {
        startNumber = totalItemCount;
        endNumber = startNumber+15;
        if(fetchclass!=null) {
            if(!fetchclass.isRunning()) {
                fetchclass= new fetchNewsClass();
                fetchclass.execute(startNumber,endNumber);
            }
        }
        else {
            fetchclass = new fetchNewsClass();
            fetchclass.execute(startNumber,endNumber);
        }
    }
}

startNumber and endNumber is just index numbers that I use to call the webservice to get the correct data from the server. fetchclass is the AsyncTask that I use to fetch the data.

Upvotes: 0

Related Questions