Endless ListView Tutorial

In Android every thing is possible, I thought of writing this tutorial long ago. ListView is the popular widget we normally use in apps. I will discuss a simple procedure of creating a endless listview here. Normally this mechanism is very well suited for populating listview by getting it items from web and we don’t want to wait until all items in listview to be populated.

What you should know ?

1) To create a custom view

2) To create a custom listview with custom adapter for it. etc

Steps To Follow

a) Creating a custom view

I named it as EndlessListViewit should extend ListView and implement OnScrollListener, add one more interface(to communicate with activity which implements it) I gave a name EndLessListener.

Overrided onScroll method plays a vital role here,

It is called once the AbsListView is scrolled, we will implement our logic here. By comparing visible item count with total items available we may decide whether to load more items from web or not.

@Override
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount){
if(getAdapter() == null)
     return;
if(getAdapter().getCount() == 0)
     return;

int l = visibleItemCount + firstVisibleItem;

if(l >= totalItemCount && !isLoading){
     //add footer layout
     this.addFooterView(footer);

     //set progress boolean
     isLoading = true;
     
     //call interface method to load new data
     listener.loadData();
 }
}

Whereas ‘footer’ is an view, ‘isLoading’ is the progress boolean flag and ‘loadData()’ is an interface method.
Source code available @ github
Here is the screen shot of required result
Screenshot_2014-03-23-14-38-18

Enjoy …! Happy Coding …!

Advertisements