分页加载数据Demo:当客户端从服务端获取数据量过大,一般为用户考虑流量,我们都需要对数据进行分页加载(譬如图片bitmap等)
布局代码:listview
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" > 5 6 <ListView 7 android:id="@+id/listview" 8 android:layout_width="fill_parent" 9 android:layout_height="fill_parent" /> 10 11 </LinearLayout>
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_info" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:textSize="20sp" 12 android:textColor="@android:color/white" 13 android:text="分页加载的数据 10" 14 /> 15 16 </LinearLayout>
footer页脚
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="wrap_content" 5 android:orientation="horizontal" > 6 7 <ProgressBar 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 /> 11 12 <TextView 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:textSize="20sp" 16 android:textColor="@android:color/white" 17 android:text="正在加载数据..........." 18 android:layout_gravity="center_vertical" 19 /> 20 21 </LinearLayout>
模拟数据
1 package com.android.hzy.service; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class DataService { 7 8 public List<String> getData(int startPosition, int offset) { 9 List<String> data = new ArrayList<String>(); 10 for (int i = 0; i < 20; i++) { 11 data.add("分页加载的数据 " + i); 12 } 13 return data; 14 } 15 }
MainActivity
1 package com.android.hzy.pageloaddata; 2 import java.util.ArrayList; 3 import java.util.List; 4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.os.Handler; 7 import android.os.Message; 8 import android.os.SystemClock; 9 import android.util.Log; 10 import android.view.View; 11 import android.widget.AbsListView; 12 import android.widget.AbsListView.OnScrollListener; 13 import android.widget.ArrayAdapter; 14 import android.widget.ListView; 15 16 import com.android.hzy.service.DataService; 17 18 public class MainActivity extends Activity { 19 20 private static final int SUCCESS_GET_DATA = 0; 21 22 private ListView listview; 23 private DataService service; 24 private List<String> data; // 加载的总数据 25 private ArrayAdapter<String> adapter; 26 27 private boolean finish = true; // 是否加载完成 28 private View footer; 29 30 private Handler mHandler = new Handler(){ 31 32 @Override 33 public void handleMessage(Message msg) { 34 switch (msg.what) { 35 case SUCCESS_GET_DATA: 36 ArrayList<String> result = (ArrayList<String>)msg.obj; 37 data.addAll(result); 38 adapter.notifyDataSetChanged(); // 让listview自动刷新 39 finish = true; // 加载完成 40 if(listview.getFooterViewsCount() > 0){ // 当页脚大于0 就删除页脚 41 listview.removeFooterView(footer); 42 } 43 break; 44 45 default: 46 break; 47 } 48 } 49 50 }; 51 52 @Override 53 protected void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 setContentView(R.layout.activity_main); 56 57 listview = (ListView) findViewById(R.id.listview); 58 service = new DataService(); 59 data = new ArrayList<String>(); 60 61 List<String> result = service.getData(0, 20); 62 data.addAll(result); 63 adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item, R.id.tv_info, data); 64 65 footer = View.inflate(getApplicationContext(), R.layout.footer, null); 66 67 listview.addFooterView(footer); // 这里必须添加页脚让setAdapter里面满足 if (mHeaderViewInfos.size() > 0|| mFooterViewInfos.size() > 0)条件 68 listview.setAdapter(adapter); 69 listview.removeFooterView(footer);// 移除掉 70 71 // 设置listview的滚动监听 72 listview.setOnScrollListener(new MyOnScrollListener()); 73 74 75 76 } 77 78 private final class MyOnScrollListener implements OnScrollListener{ 79 80 81 private int countPage = 5; // 加载的总页数 82 private int pageSize = 20; // 每页加载20条数据 83 84 @Override 85 public void onScrollStateChanged(AbsListView view, int scrollState) { 86 // TODO Auto-generated method stub 87 Log.i("i", " scrollState " + scrollState); 88 } 89 90 @Override 91 public void onScroll(AbsListView view, int firstVisibleItem, 92 int visibleItemCount, int totalItemCount) { 93 // TODO Auto-generated method stub 94 Log.i("i", " firstVisibleItem:" + firstVisibleItem + ",visibleItemCount:" + visibleItemCount 95 + ",totalItemCount:" + totalItemCount); 96 97 final int totalCount = firstVisibleItem + visibleItemCount; // 已经加载了多少条目 98 int currentPage = totalCount/pageSize; // 当前页 99 int nextPage = currentPage + 1; // 下一页 100 if (totalCount == totalItemCount && nextPage <= countPage && finish) {// 已经移动到了listview的最后一个条目去加载 101 102 finish = false; // 加载未完成 103 // 加载数据时 添加页脚 104 listview.addFooterView(footer); 105 106 new Thread() { 107 public void run() { 108 SystemClock.sleep(3000); 109 List<String> result = service.getData(totalCount + 1, pageSize);// 110 // 发送消息给hander 111 Message msg = new Message(); 112 msg.what = SUCCESS_GET_DATA; 113 msg.obj = result; 114 mHandler.sendMessage(msg); 115 }; 116 }.start(); 117 } 118 } 119 120 } 121 122 }