listview分頁加載數據

listview底部的視圖:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   <Button      
  7.       android:id="@+id/bt_load"      
  8.       android:layout_width="fill_parent"      
  9.       android:layout_height="wrap_content"    
  10.       android:text="加載更多數據" />   
  11.   <ProgressBar  
  12.       android:id="@+id/pg"  
  13.       android:layout_width="wrap_content"  
  14.       android:layout_height="wrap_content"  
  15.       android:layout_gravity="center_horizontal"  
  16.       android:visibility="gone"  
  17.       />  
  18. </LinearLayout> 
listview的每個item的視圖:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <TextView  
  8.         android:id="@+id/tv_title"  
  9.         android:textSize="20sp"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_marginTop="5dp"  
  13.         />  
  14.     <TextView  
  15.         android:textSize="12sp"  
  16.         android:id="@+id/tv_content"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_marginTop="5dp"  
  20.         />  
  21.   
  22. </LinearLayout>
主界面就一個listview,就不貼了

Activity的代碼

  1. public class MoreDateListActivity extends Activity implements OnScrollListener {  
  2.       
  3.     // ListView的Adapter  
  4.     private SimpleAdapter mSimpleAdapter;  
  5.     private ListView lv;  
  6.     private Button bt;  
  7.     private ProgressBar pg;  
  8.     private ArrayList<HashMap<String,String>> list;  
  9.     // ListView底部View  
  10.     private View moreView;  
  11.     private Handler handler;  
  12.     // 設置一個最大的數據條數,超過即不再加載  
  13.     private int MaxDateNum;  
  14.     // 最後可見條目的索引  
  15.     private int lastVisibleIndex;  
  16.       
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.           
  23.           
  24.         MaxDateNum = 22// 設置最大數據條數  
  25.   
  26.         lv = (ListView) findViewById(R.id.lv);  
  27.   
  28.         // 實例化底部佈局  
  29.         moreView = getLayoutInflater().inflate(R.layout.moredate, null);  
  30.   
  31.         bt = (Button) moreView.findViewById(R.id.bt_load);  
  32.         pg = (ProgressBar) moreView.findViewById(R.id.pg);  
  33.         handler = new Handler();  
  34.   
  35.         // 用map來裝載數據,初始化10條數據  
  36.         list = new ArrayList<HashMap<String,String>>();  
  37.         for (int i = 0; i < 10; i++) {  
  38.             HashMap<String, String> map = new HashMap<String, String>();  
  39.             map.put("ItemTitle""第" + i + "行標題");  
  40.             map.put("ItemText""第" + i + "行內容");  
  41.             list.add(map);  
  42.         }  
  43.         // 實例化SimpleAdapter  
  44.         mSimpleAdapter = new SimpleAdapter(this, list, R.layout.item,  
  45.                 new String[] { "ItemTitle""ItemText" },  
  46.                 new int[] { R.id.tv_title, R.id.tv_content });  
  47.         // 加上底部View,注意要放在setAdapter方法前  
  48.         lv.addFooterView(moreView);  
  49.         lv.setAdapter(mSimpleAdapter);  
  50.         // 綁定監聽器  
  51.         lv.setOnScrollListener(this);  
  52.   
  53.         bt.setOnClickListener(new OnClickListener() {  
  54.   
  55.             @Override  
  56.             public void onClick(View v) {  
  57.                 pg.setVisibility(View.VISIBLE);// 將進度條可見  
  58.                 bt.setVisibility(View.GONE);// 按鈕不可見  
  59.   
  60.                 handler.postDelayed(new Runnable() {  
  61.   
  62.                     @Override  
  63.                     public void run() {  
  64.                         loadMoreDate();// 加載更多數據  
  65.                         bt.setVisibility(View.VISIBLE);  
  66.                         pg.setVisibility(View.GONE);  
  67.                         mSimpleAdapter.notifyDataSetChanged();// 通知listView刷新數據  
  68.                     }  
  69.   
  70.                 }, 2000);  
  71.             }  
  72.         });  
  73.   
  74.     }  
  75.   
  76.     private void loadMoreDate() {  
  77. //若是網絡客戶端,則需要開啓異步任務獲取下一頁的網絡數據
  78.         int count = mSimpleAdapter.getCount();  
  79.         if (count + 5 < MaxDateNum) {  
  80.             // 每次加載5條  
  81.             for (int i = count; i < count + 5; i++) {  
  82.                 HashMap<String, String> map = new HashMap<String, String>();  
  83.                 map.put("ItemTitle""新增第" + i + "行標題");  
  84.                 map.put("ItemText""新增第" + i + "行內容");  
  85.                 list.add(map);  
  86.             }  
  87.         } else {  
  88.             // 數據已經不足5條  
  89.             for (int i = count; i < MaxDateNum; i++) {  
  90.                 HashMap<String, String> map = new HashMap<String, String>();  
  91.                 map.put("ItemTitle""新增第" + i + "行標題");  
  92.                 map.put("ItemText""新增第" + i + "行內容");  
  93.                 list.add(map);  
  94.             }  
  95.         }  
  96.   
  97.     }  
  98.   
  99.     @Override  
  100.     public void onScroll(AbsListView view, int firstVisibleItem,  
  101.             int visibleItemCount, int totalItemCount) {  
  102.         // 計算最後可見條目的索引  
  103.         lastVisibleIndex = firstVisibleItem + visibleItemCount - 1;  
  104.   
  105.         // 所有的條目已經和最大條數相等,則移除底部的View  
  106.         if (totalItemCount == MaxDateNum + 1) {  
  107.             lv.removeFooterView(moreView);  
  108.             Toast.makeText(this"數據全部加載完成,沒有更多數據!", Toast.LENGTH_LONG).show();  
  109.         }  
  110.   
  111.     }  
  112.   
  113.     @Override  
  114.     public void onScrollStateChanged(AbsListView view, int scrollState) {  
  115.         // 滑到底部後自動加載,判斷listview已經停止滾動並且最後可視的條目等於adapter的條目  
  116.         if (scrollState == OnScrollListener.SCROLL_STATE_IDLE  
  117.                 && lastVisibleIndex == mSimpleAdapter.getCount()) {  
  118.             // 當滑到底部時自動加載  
  119.             // pg.setVisibility(View.VISIBLE);  
  120.             // bt.setVisibility(View.GONE);  
  121.             // handler.postDelayed(new Runnable() {  
  122.             //  
  123.             // @Override  
  124.             // public void run() {  
  125.             // loadMoreDate();  
  126.             // bt.setVisibility(View.VISIBLE);  
  127.             // pg.setVisibility(View.GONE);  
  128.             // mSimpleAdapter.notifyDataSetChanged();  
  129.             // }  
  130.             //  
  131.             // }, 2000);  
  132.   
  133.         }  
  134.   
  135.     }  
  136.       
  137. }  
listview綁定了onScrollListener監聽器,並且實現了onScroll和onScrollStateChanged方法。

通過判斷listview已經停止滾動並且最後可視的條目等於adapter的條目,可以知道用戶已經滑動到底部並且自動加載,代碼中將這部分代碼註釋掉了,大家可以自己試下。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章