To dynamically load more items to the ListView when you scroll at the bottom, we implement onScroll() method in which we can find out the which item is visible when we will reach at bottom it will load more items.
Following steps are used for this:
Adding footer view on ListView: For this we will create an XML file that will define how the footer of your ListView will look like.
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:gravity="center_horizontal" android:padding="3dp" android:layout_height="fill_parent">
<textview android:id="@id/android:empty" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="center" android:padding="5dp" android:text="Loading...">
Now the java code for loading more items in ListView is as follow:
package com.android;
import java.util.ArrayList;
import org.json.JSONArray;
import android.app.Activity;
import android.widget.ListView;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
public class Home extends Activity
{
private boolean isLoadMore = false;
private ArrayList itemList = new ArrayList();
private MyAdapter adapter = null;
private ListView listView;
private View footerView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
footerView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null, false);
// Implement on scroll listener to load more items
listView.setOnScrollListener(new OnScrollListener(){
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Do nothing
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int lastInScreen = firstVisibleItem + visibleItemCount;
if((lastInScreen == totalItemCount)){
if(isLoadMore == false)
{
if(loadMoreTextViewLength != 0)
{
isLoadMore = true;
// Add footer to ListView
list.addFooterView(loadingFooter);
additems();
}
}
}
}
});
// Show list in listView
additems();
}
// Add items into list view after getting from server
private void additems()
{
GetList getList = new GetList();
getList.execute();
}
/**
* This represents a asynchronous task used to
* get list from server
*
*/
private class GetList extends AsyncTask
{
@Override
protected String doInBackground(String...urls)
{
String response = null;
try
{
// Hit the API to get list of items that you want to show in ListView
return response;
}
catch(JSONException e)
{
e.printStackTrace();
return null;
}
}
@Override
protected void onPreExecute()
{
}
protected void onPostExecute(String response)
{
try {
if(response != null)
{
if(response.equals(""))
{
MyUtility.saveFlagFromMessageThread(homeActivity, false);
fromPushNotification = false;
if(isLoadMore)
{
listViwe.removeFooterView(loadingFooter);
isLoadMore = false;
}
adapter.notifyDataSetChanged();
}
else
{
if(!response.equals("null"))
{
if(isLoadMore)
{
listViwe.removeFooterView(loadingFooter);
isLoadMore = false;
}
JSONArray jsonArray = new JSONArray(response);
loadMoreTextViewLength = jsonArray.length();
if(jsonArray.length() > 0)
{
// add items into Arralist
adapter = new MyAdapter(context, itemList);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
if(isLoadMore)
{
listViwe.removeFooterView(loadingFooter);
isLoadMore = false;
}
}
}
}
}
Please feel free to share the experiences you had while dynamicaly load more items to the ListView as this will help other people who read the post.
0 Comment(s)