article from : http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview-never-ending-list/
We pretty much all know the nice technique apps like GMail / Facebook use to dynamically load more items to the ListView then you scroll to the bottom. In this post i want to shed some light on how to make such a list ourselves!
We can approach this from a few sides:
- The first option is to add a button to the footer of the view. when pressed it will load more content.
- The second option is to check if the last item is visible and load more content when it is!
I really like the second one, because i think its more user friendly. Android makes checking is a item is in view very easy because they added a function with the following method signature:
1 |
public abstract void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) |
As you might have guessed: with these parameters we can easily figure out whats the last item on screen! Before we make this method we will first have to add a footer to the ListView.
More after the breaky break!
Adding a FooterView to the ListView
A footer View is nothing more than a piece of XML that defines how the footer of your listview will look like. Mine is as follows:
02 |
android:orientation = "horizontal" |
03 |
android:layout_width = "fill_parent" |
04 |
android:gravity = "center_horizontal" |
06 |
android:layout_height = "fill_parent" > |
08 |
android:id = "@id/android:empty" |
09 |
android:layout_width = "wrap_content" |
10 |
android:layout_height = "fill_parent" |
11 |
android:gravity = "center" |
13 |
android:text = "Loading more days..." /> |
Its just a simple textview with a message for demonstration purposes. But you can put anything in here
Adding the View to the ListView is not hard. The only thing you need to be aware of is that you put it above the line that adds your adapter to the View! We will use this code to add it to our ListView: addFooterView(View)
2 |
View footerView = ((LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null , false ); |
3 |
this .getListView().addFooterView(footerView); |
Now the fun part, checking if we are all the way down and loading the items.
Checking what items are visible in the ListView
As stated above, we have a nice method to do the checking. Here is the code that does all the magic tricks:
02 |
this .getListView().setOnScrollListener( new OnScrollListener(){ |
05 |
public void onScrollStateChanged(AbsListView view, int scrollState) {} |
08 |
public void onScroll(AbsListView view, int firstVisibleItem, |
09 |
int visibleItemCount, int totalItemCount) { |
11 |
int lastInScreen = firstVisibleItem + visibleItemCount; |
13 |
if ((lastInScreen == totalItemCount) && !(loadingMore)){ |
14 |
Thread thread = new Thread( null , loadMoreListItems); |
The actual loading is done in a separate thread (Runnable). This way we don't lock the main interface ( GUI ).
Implementing Runnables to handle the loading
The loading is done by 2 Runnables. The 1st runnable ( loadMoreListItems )is called to get the data and the 2nd runnable ( returnRes ) is called on the main (UI) thread to update the interface. To switch from the 1st to the 2nd we use a method named: runOnUiThread
Here is the code that implements the Runnables, i commented the code as much as i could to make it understandable:
02 |
private Runnable loadMoreListItems = new Runnable() { |
08 |
myListItems = new ArrayList<String>(); |
10 |
try { Thread.sleep( 1000 ); |
11 |
} catch (InterruptedException e) {} |
13 |
for ( int i = 0 ; i < itemsPerPage; i++) { |
15 |
myListItems.add( "Date: " + (d.get(Calendar.MONTH)+ 1 ) + "/" + d.get(Calendar.DATE) + "/" + d.get(Calendar.YEAR) ); |
17 |
d.add(Calendar.DATE, 1 ); |
20 |
runOnUiThread(returnRes); |
And the 2nd Runnable:
02 |
private Runnable returnRes = new Runnable() { |
06 |
if (myListItems != null && myListItems.size() > 0 ){ |
07 |
for ( int i= 0 ;i < myListItems.size();i++) |
08 |
adapter.add(myListItems.get(i)); |
11 |
setTitle( "Neverending List with " + String.valueOf(adapter.getCount()) + " items" ); |
13 |
adapter.notifyDataSetChanged(); |
This is all the magic, for the entire implementation is suggest you download the eclipse project below and mess around with it!