original from:
http://blog.csdn.net/mayingcai1987/article/details/6273606
I added some personal note to the original code , so , just notice the notes as below:
package com.dynamic_load; import android.app.Activity; import android.app.ListActivity; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.*; import android.widget.AbsListView.OnScrollListener; import android.widget.LinearLayout.LayoutParams; public class DynamicLoadActivity extends ListActivity { private LinearLayout loadLayout; private ListView listView; private ListViewAdapter listViewAdapter=new ListViewAdapter(); private int lastItem=0; private int maxCount=41; private final Handler handler=new Handler(); private final LayoutParams progressBarLayoutParams=new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); private final LayoutParams tipContentLayoutParams=new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); private class ListViewAdapter extends BaseAdapter{ public int showCount=10; public int getCount() { // throw new UnsupportedOperationException("Not supported yet."); //the total list count of the ListView //triggered when an item appear and dispear; //including appear on the view at the first time; //return the total count of the appear item; //ListView gets this count to appear the items; // Log.i("l","get count:"+count); return showCount; } public Object getItem(int position) { // throw new UnsupportedOperationException("Not supported yet."); // Log.i("l","get item:"+position); return position; } public long getItemId(int position) { // throw new UnsupportedOperationException("Not supported yet."); //triggered when click an item; //return the item id which was clicked; // Log.i("l","get itemId:"+position); return position; } //set the elements bar of the list view public View getView(int position, View view, ViewGroup parent) { // throw new UnsupportedOperationException("Not supported yet."); // Log.i("l","get view:"); //return the view item to show up to the ListView to show; //when a view is going to show up , ListView will trigger this //function to render a view that it returned; TextView textView; if(view==null){ textView=new TextView(DynamicLoadActivity.this); } else { textView=(TextView)view; } textView.setText("Item"+position); textView.setTextSize(20f); textView.setGravity(Gravity.CENTER); textView.setHeight(60); return textView; } } private OnScrollListener onScrollListener=new OnScrollListener(){ public void onScrollStateChanged(AbsListView view, int scrollState) { // throw new UnsupportedOperationException("Not supported yet."); // Callback method to be invoked while the list view or grid view is being scrolled. // If the view is being scrolled, this method will be called before the next frame // of the scroll is rendered. In particular, it will be called before any calls to // getView(int, View, ViewGroup). // Parameters // view The view whose scroll state is being reported // scrollState The current scroll state. One of SCROLL_STATE_IDLE, // SCROLL_STATE_TOUCH_SCROLL or SCROLL_STATE_IDLE. //that means from still to scrolling, it will trigger once. //or from scrolling to scroll,it will also trigger once. Log.i("l","onScrollStateChanged"); if(lastItem==listViewAdapter.showCount //if lastItem==10; && scrollState==OnScrollListener.SCROLL_STATE_IDLE){ //&& the view is not scroll if(listViewAdapter.showCount<=maxCount){ Runnable r=new Runnable(){ public void run() { // throw new UnsupportedOperationException("Not supported yet."); //since the count has been changed , and the count will be return to the //ListView from OnScrollListener , so listener must notify the listView //that something return to it has been changed, but not changed by //ListView itself , but by the other classes; listViewAdapter.showCount+=10; //so this function must be followed the data that has been changed , which //dosent changed by ListView itself; listViewAdapter.notifyDataSetChanged(); listView.setSelection(lastItem); } }; handler.postDelayed(r, 1000); } } } public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // throw new UnsupportedOperationException("Not supported yet."); // Callback method to be invoked when the list or grid has been scrolled. // This will be called after the scroll has completed // Parameters // view The view whose scroll state is being reported // firstVisibleItem the index of the first visible cell (ignore if visibleItemCount == 0) // visibleItemCount the number of visible cells // totalItemCount the number of items in the list adaptor //that just means the view scrolling , it will trigger once on every frame //when it is still. it will not trigger . Trigger only it is moving. Log.i("l","onScroll"); Log.i("l","firstVisibleItem:"+firstVisibleItem); Log.i("l","visibleItemCount:"+visibleItemCount); //even only a little part is visible , will be calculate into the visibleItemCount; //on opposite, only a part can't be seen copletely , that will be token out of the //visibleItemCount; //calculate every frame! lastItem=firstVisibleItem+visibleItemCount-1; if(listViewAdapter.showCount>maxCount){ //.count=10 original; maxCount=41; listView.removeFooterView(loadLayout); } } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); //set the foot view of the list view loadLayout=new LinearLayout(this); loadLayout.setMinimumHeight(60); loadLayout.setGravity(Gravity.CENTER); loadLayout.setOrientation(LinearLayout.HORIZONTAL); ProgressBar progressBar=new ProgressBar(this); progressBar.setPadding(0,0,15,0); loadLayout.addView(progressBar,progressBarLayoutParams); TextView tipContent=new TextView(this); tipContent.setText("Loading..."); loadLayout.addView(tipContent,tipContentLayoutParams); //the footer view is not always show on the screen! //it exists under the last item of the .getCount return; listView=this.getListView(); listView.addFooterView(loadLayout); this.setListAdapter(listViewAdapter); listView.setOnScrollListener(onScrollListener); } }