/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
private void loadMoreDate() { int count = mSimpleAdapter.getCount(); if (count + 5 // 每次加载5条 for (int i = count; i HashMap map = new HashMap(); map.put("ItemTitle", "新增第" + i + "行标题"); map.put("ItemText", "新增第" + i + "行内容"); list.add(map); } } else { // 数据已经不足5条 for (int i = count; i HashMap map = new HashMap(); map.put("ItemTitle", "新增第" + i + "行标题"); map.put("ItemText", "新增第" + i + "行内容"); list.add(map); } }
} /** * firstVisibleItem 表示在当前屏幕显示的第一个listItem在整个listView里面的位置(下标从0开始) * visibleItemCount表示在现时屏幕可以见到的ListItem(部分显示的ListItem也算)总数 * totalItemCount表示ListView的ListItem总数 * listView.getLastVisiblePosition()表示在现时屏幕最后一个ListItem * (最后ListItem要完全显示出来才算)在整个ListView的位置(下标从0开始) */ @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // 计算最后可见条目的索引 lastVisibleIndex = firstVisibleItem + visibleItemCount - 1;
/** *scrollState有三种状态,分别是SCROLL_STATE_IDLE、SCROLL_STATE_TOUCH_SCROLL、SCROLL_STATE_FLING *SCROLL_STATE_IDLE是当屏幕停止滚动时 *SCROLL_STATE_TOUCH_SCROLL是当用户在以触屏方式滚动屏幕并且手指仍然还在屏幕上时(The user is scrolling using touch, and their finger is still on the screen) *SCROLL_STATE_FLING是当用户由于之前划动屏幕并抬起手指,屏幕产生惯性滑动时(The user had previously been scrolling using touch and had performed a fling) */ @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // 滑到底部后自动加载,判断listview已经停止滚动并且最后可视的条目等于adapter的条目 if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && lastVisibleIndex == mSimpleAdapter.getCount()) { // 当滑到底部时自动加载 // pg.setVisibility(View.VISIBLE); // bt.setVisibility(View.GONE); // handler.postDelayed(new Runnable() { // // @Override // public void run() { // loadMoreDate(); // bt.setVisibility(View.VISIBLE); // pg.setVisibility(View.GONE); // mSimpleAdapter.notifyDataSetChanged(); // } // // }, 2000);