This time, the Code is better than the previous one. After the full data is loaded, A textview with "loaded all" is displayed at the bottom of the lisview. You can compare my previous blog
Android Listview batch loading + automatic loading (with source code download) to see how different the code is
Directly run the Code:
Public class TestForListviewActivity extends Activity implementsOnScrollListener {private View mFooterView; private LinearLayout mloadingLinear; // viewprivate TextView mLoadFinishTextView displayed when loading; // viewprivate final int LOAD_STATE_IDLE = 0 after loading all the data; // It is not loaded, and private final int LOAD_STATE_LOADING = 1 is not loaded on the server; // loading status private final int LOAD_STATE_FINISH = 2; // indicates that all data on the server has been loaded. private int loadState = LOAD_STATE_IDLE; // record the loading status private final int MAX_COUNT = 15; // indicates that the server has a total of MAX_COUNT data private final int EACH_COUNT = 10; // indicates the number of items loaded each time. private ListView mListview = null; private PaginationAdapter mAdapter; private Handler handler = new Handler (); @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); mFooterView = getLayoutInflater (). inflate (R. layout. loadmore, null); mloadingLinear = (LinearLayout) mFooterView. findViewById (R. id. loading_linear); mLoadFinishTextView = (TextView) mFooterView. findViewById (R. id. load_finish_textview); mListview = (ListView) findViewById (R. id. listview); mListview. addFooterView (mFooterView); // sets the List bottom view List
News = new ArrayList
(); MAdapter = new PaginationAdapter (news); mListview. setAdapter (mAdapter); // when setOnScrollListener is set, the onscroll method is automatically called. MListview. setOnScrollListener (this);} public void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {Log. ("onS", "firstVisibleItem" + firstVisibleItem + "visibleItemCount" + visibleItemCount + "totalItemCount" + totalItemCount); if (firstVisibleItem + visibleItemCount = totalItemCount) {if (loadState = LOAD_STATE_IDLE) {Log. I ("onScroll", "firstVisibleItem" + firstVisibleItem + "visibleItemCount" + visibleItemCount + "totalItemCount" + totalItemCount); loadState = LOAD_STATE_LOADING; loadMore ();}}} public void onScrollStateChanged (AbsListView arg0, int scrollState) {Log. I ("onScrollStateChanged", scrollState + "");} private void loadMore () {// wait 2 seconds before loading. The wait time of the simulated network is 2shandler. postDelayed (new Runnable () {public void run () {loadData (); mAdapter. notifyDataSetChanged (); if (loadState = LOAD_STATE_FINISH) {// after loading all data, a "loaded all" textviewmloadingLinear is displayed at the bottom. setVisibility (View. GONE); mLoadFinishTextView. setVisibility (View. VISIBLE) ;}}}, 2000) ;}private void loadData () {int dataIndex; // index of the data to be loaded (starting from 0) int count = mAdapter. getCount (); // if the server still has data, load more for (dataIndex = count; dataIndex <Math. min (count + EACH_COUNT, MAX_COUNT); dataIndex ++) {News item = new News (); item. setTitle ("Title" + dataIndex); item. setContent ("This is News Content" + dataIndex); mAdapter. addNewsItem (item);} // if all the data on the server has been loaded, if (dataIndex = MAX_COUNT) {loadState = LOAD_STATE_FINISH;} else {loadState = LOAD_STATE_IDLE ;}} class PaginationAdapter extends BaseAdapter {List
NewsItems; public PaginationAdapter (List
Newsitems) {this. newsItems = newsitems;} public int getCount () {return newsItems = null? 0: newsItems. size ();} public Object getItem (int position) {return newsItems. get (position);} public long getItemId (int position) {return position;} public void addNewsItem (News newsitem) {newsItems. add (newsitem);} public View getView (int position, View convertView, ViewGroup parent) {if (convertView = null) {convertView = getLayoutInflater (). inflate (R. layout. list_item, null);} // news title TextView tvTitle = (TextView) convertView. findViewById (R. id. newstitle); tvTitle. setText (newsItems. get (position ). getTitle (); // news content TextView tvContent = (TextView) convertView. findViewById (R. id. newscontent); tvContent. setText (newsItems. get (position ). getContent (); return convertView ;}}}
Demo download for release: source code download