listview底部的視圖:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <Button
- android:id="@+id/bt_load"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="加載更多數據" />
- <ProgressBar
- android:id="@+id/pg"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:visibility="gone"
- />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/tv_title"
- android:textSize="20sp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="5dp"
- />
- <TextView
- android:textSize="12sp"
- android:id="@+id/tv_content"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="5dp"
- />
- </LinearLayout>
Activity的代碼
- public class MoreDateListActivity extends Activity implements OnScrollListener {
- // ListView的Adapter
- private SimpleAdapter mSimpleAdapter;
- private ListView lv;
- private Button bt;
- private ProgressBar pg;
- private ArrayList<HashMap<String,String>> list;
- // ListView底部View
- private View moreView;
- private Handler handler;
- // 設置一個最大的數據條數,超過即不再加載
- private int MaxDateNum;
- // 最後可見條目的索引
- private int lastVisibleIndex;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- MaxDateNum = 22; // 設置最大數據條數
- lv = (ListView) findViewById(R.id.lv);
- // 實例化底部佈局
- moreView = getLayoutInflater().inflate(R.layout.moredate, null);
- bt = (Button) moreView.findViewById(R.id.bt_load);
- pg = (ProgressBar) moreView.findViewById(R.id.pg);
- handler = new Handler();
- // 用map來裝載數據,初始化10條數據
- list = new ArrayList<HashMap<String,String>>();
- for (int i = 0; i < 10; i++) {
- HashMap<String, String> map = new HashMap<String, String>();
- map.put("ItemTitle", "第" + i + "行標題");
- map.put("ItemText", "第" + i + "行內容");
- list.add(map);
- }
- // 實例化SimpleAdapter
- mSimpleAdapter = new SimpleAdapter(this, list, R.layout.item,
- new String[] { "ItemTitle", "ItemText" },
- new int[] { R.id.tv_title, R.id.tv_content });
- // 加上底部View,注意要放在setAdapter方法前
- lv.addFooterView(moreView);
- lv.setAdapter(mSimpleAdapter);
- // 綁定監聽器
- lv.setOnScrollListener(this);
- bt.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- 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();// 通知listView刷新數據
- }
- }, 2000);
- }
- });
- }
- private void loadMoreDate() {
- //若是網絡客戶端,則需要開啓異步任務獲取下一頁的網絡數據
- int count = mSimpleAdapter.getCount();
- if (count + 5 < MaxDateNum) {
- // 每次加載5條
- for (int i = count; i < count + 5; i++) {
- HashMap<String, String> map = new HashMap<String, String>();
- map.put("ItemTitle", "新增第" + i + "行標題");
- map.put("ItemText", "新增第" + i + "行內容");
- list.add(map);
- }
- } else {
- // 數據已經不足5條
- for (int i = count; i < MaxDateNum; i++) {
- HashMap<String, String> map = new HashMap<String, String>();
- map.put("ItemTitle", "新增第" + i + "行標題");
- map.put("ItemText", "新增第" + i + "行內容");
- list.add(map);
- }
- }
- }
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
- // 計算最後可見條目的索引
- lastVisibleIndex = firstVisibleItem + visibleItemCount - 1;
- // 所有的條目已經和最大條數相等,則移除底部的View
- if (totalItemCount == MaxDateNum + 1) {
- lv.removeFooterView(moreView);
- Toast.makeText(this, "數據全部加載完成,沒有更多數據!", Toast.LENGTH_LONG).show();
- }
- }
- @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);
- }
- }
- }
通過判斷listview已經停止滾動並且最後可視的條目等於adapter的條目,可以知道用戶已經滑動到底部並且自動加載,代碼中將這部分代碼註釋掉了,大家可以自己試下。