现在的位置: 首页 > 综合 > 正文

android 实现listview的分页加载

2014年01月12日 ⁄ 综合 ⁄ 共 3022字 ⁄ 字号 评论关闭
上篇博文和大家分享了下拉刷新,这是一个用户体验非常好的操作方式。新浪微薄就是使用这种方式的典型。

还有个问题,当用户从网络上读取微薄的时候,如果一下子全部加载用户未读的微薄这将耗费比较长的时间,造成不好的用户体验,同时一屏的内容也不足以显示如此多的内容。这时候,我们就需要用到另一个功能,那就是listview的分页了。通过分页分次加载数据,用户看多少就去加载多少。

通常这也分为两种方式,一种是设置一个按钮,用户点击即加载。另一种是当用户滑动到底部时自动加载。今天我就和大家分享一下这个功能的实现。

首先,写一个xml文件,moredata.xml,该文件即定义了放在listview底部的视图:

[html] view
plain
copy

  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>  

可以看到是一个按钮和一个进度条。因为只做一个演示,这里简单处理,通过设置控件的visibility,未加载时显示按钮,加载时就显示进度条。

写一个item.xml,大家应该很熟悉了。用来定义listview的每个item的视图。

[html] view
plain
copy

  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>  

main.xml就不贴了,整个主界面就一个listview。

直接先看下Activity的代码,在里面实现分页效果。

[java] view
plain
copy

  1. package com.notice.moredate;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.os.Handler;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.AbsListView;  
  12. import android.widget.AbsListView.OnScrollListener;  
  13. import android.widget.Button;  
  14. import android.widget.ListView;  
  15. import android.widget.ProgressBar;  
  16. import android.widget.SimpleAdapter;  
  17. import android.widget.Toast;  
  18.   
  19. public class MoreDateListActivity extends Activity implements OnScrollListener {  
  20.       
  21.     // ListView的Adapter  
  22.     private SimpleAdapter mSimpleAdapter;  
  23.     private ListView lv;  
  24.     private Button bt;  
  25.     private ProgressBar pg;  
  26.     private ArrayList<HashMap<String,String>> list;  
  27.     // ListView底部View  
  28.     private View moreView;  
  29.     private Handler handler;  
  30.     // 设置一个最大的数据条数,超过即不再加载  
  31.     private int MaxDateNum;  
  32.     // 最后可见条目的索引  
  33.     private int lastVisibleIndex;  
  34.       
  35.     /** Called when the activity is first created. */  
  36.     @Override  
  37.     public void onCreate(Bundle savedInstanceState) {  
  38.         super

抱歉!评论已关闭.