|
有两种方法:
我在项目中用了两种方法,来进行listView的分页,分别是用Handler和AsyncTask。
第一种方法:用Handler和线程
看代码:
java代码
package com.cnjmwl.scm.salesorder;
import java.util.ArrayList;
import com.cnjmwl.scm.MainActivity;
import com.cnjmwl.scm.R;
import com.cnjmwl.util.HttpUtil;
import com.cnjmwl.util.ReadXML;
import com.cnjmwl.util.StringUtil;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout.LayoutParams;
import com.cnjmwl.scm.entity.*;
public class SalesOrderActivity extends Activity implements OnScrollListener{
private ListView mListView;
private MyAdapter mMyAdapter;
LinearLayout loadingLayout;
private Thread mThread;
/**
* 设置布局显示属性
*/
private LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
/**
* 设置布局显示目标最大化属性
*/
private LayoutParams FFlayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
private ProgressBar progressBar;
/**
* 报检属性名称
*/
String[] strID,strUnit,strStatus,strStatusDesc,strNO,strDate,strLinkMan,strLinkTel,business;
int maxCount;//数据库中的订单总数
ArrayList<SalesOrder> lists;
View.OnClickListener cl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order_main);
setupViews();
}
public void setupViews(){
if(maxCount==0){
addDeclaration();
}
//线性布局
LinearLayout layout=new LinearLayout(this);
//设置布局水平方向
layout.setOrientation(LinearLayout.HORIZONTAL);
//进度条
progressBar=new ProgressBar(this);
//把进度条加入到layout中
layout.addView(progressBar,mLayoutParams);
//文本内容
TextView textView=new TextView(this);
textView.setText("加载中...");
textView.setGravity(Gravity.CENTER_VERTICAL);
//把文本加入到layout中
layout.addView(textView,FFlayoutParams);
//设置layout的重力方向,即对齐方式是
layout.setGravity(Gravity.CENTER);
//设置ListView的页脚layout
loadingLayout = new LinearLayout(this);
loadingLayout.addView(layout,mLayoutParams);
loadingLayout.setGravity(Gravity.CENTER);
//得到一个ListView用来显示条目
mListView=(ListView)findViewById(R.id.listview);
//添加到页脚
mListView.addFooterView(loadingLayout);
mMyAdapter=new MyAdapter();
mListView.setAdapter(mMyAdapter);
mListView.setOnScrollListener(this);
mListView.setTextFilterEnabled(true);
mListView.setBackgroundResource(R.drawable.background);
}
/**
* 向服务器请求待办列表
*/
private String queryList(String uri){
String url = HttpUtil.BASE_URL+uri;
System.out.println(url);
return HttpUtil.getHTTPUtil().queryStringForPost(url);
}
public void addDeclaration(){
String result2=queryList("orderStatusList".trim());
lists=ReadXML.readXML(result2).getInspection2();
Log.v("result2--", result2);
strID=new String[lists.size()];
strNO=new String[lists.size()];
strStatus=new String[lists.size()];
strStatusDesc=new String[lists.size()];
strUnit=new String[lists.size()];
strDate=new String[lists.size()];
strLinkMan=new String[lists.size()];
strLinkTel=new String[lists.size()];
business=new String[lists.size()];
int i2=0;
for(SalesOrder in:lists){
strID[i2]=in.getId();
strStatus[i2]=in.getStatus();
strDate[i2]=in.getDate();
strLinkTel[i2]=in.getLinkTel();
//business[i2]=business[i2].replace("@", "\n");
if(in.getStatusDesc()!=null&&!in.getStatusDesc().equals("")){
strStatusDesc[i2]=StringUtil.unicodeToUtf8(in.getStatusDesc());
}else {
strStatusDesc[i2]="";
}
if(in.getUnit()!=null&&!in.getUnit().equals("")){
strUnit[i2]=StringUtil.unicodeToUtf8(in.getUnit());
}else {
strUnit[i2]="";
}
if(in.getLinkMan()!=null&&!in.getLinkMan().equals("")){
strLinkMan[i2]=StringUtil.unicodeToUtf8(in.getLinkMan());
}else {
strLinkMan[i2]="";
}
if(in.getBusinessItems()!=null&& !in.getBusinessItems().equals("")){
business[i2]=StringUtil.unicodeToUtf8(in.getBusinessItems());
business[i2]=business[i2].replace("||", "\n");
}else {
business[i2]="";
}
//business[i2]=business[i2].replace("|", "\n");
Log.v("business", business[i2]);
strNO[i2]=in.getNo();
i2++;
}
maxCount=strID.length;
}
/**
* 创建菜单
*
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, Menu.FIRST+1, 5, "返回首页").setIcon(android.R.drawable.ic_menu_delete);
menu.add(Menu.NONE, Menu.FIRST+2, 2, "退出").setIcon(android.R.drawable.ic_menu_edit);
// this.getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
//菜单项被选择事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST + 1:
Intent intent=new Intent(Copy_4_of_SalesOrderActivity.this,MainActivity.class);
startActivity(intent);
break;
case Menu.FIRST + 2:
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
System.exit(0);
break;
}
return false;
}
//定义自己的适配器,注意getCount和getView方法
private class MyAdapter extends BaseAdapter{
int count = 10 ;
@Override
public int getCount() {
return count;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_data, null);
TextView noTextView=(TextView)convertView.findViewById(R.id.ItemNO);
noTextView.setText(strNO[position]);
TextView LinkManTextView=(TextView)convertView.findViewById(R.id.ItemLinkMan);
LinkManTextView.setText(strLinkMan[position]);
TextView LinkTelTextView=(TextView)convertView.findViewById(R.id.ItemLinkTel);
LinkTelTextView.setText(strLinkTel[position]);
TextView busTextView=(TextView)convertView.findViewById(R.id.Itembusiness1);
busTextView.setText(business[position]);
return convertView;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem + visibleItemCount == totalItemCount){
//开远程去下载网络数据
if(mThread == null || ! mThread.isAlive()){
mThread = new Thread(){
@Override
public void run() {
try {
//这里写网络数据请求的方法
if(maxCount==0){
addDeclaration();
}
} catch (Exception e) {
e.printStackTrace();
}
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
mThread.run();
}else {
mThread.destroy();
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Log.v("mMyAdapter.count", mMyAdapter.count+"");
if(mMyAdapter.count < maxCount){
if((maxCount - mMyAdapter.count) >10){
mMyAdapter.count+=10;
}else {
mMyAdapter.count+= maxCount -mMyAdapter.count;
}
int currentPage = mMyAdapter.count / 10;
//Toast.makeText(getApplicationContext(), "第"+currentPage +"页",Toast.LENGTH_LONG).show();
}else {
mListView.removeFooterView(loadingLayout);
mThread.stop();
}
//重新刷新ListView的adapter里面数据
mMyAdapter.notifyDataSetChanged();
break;
default:
break;
}
}
};
}
复制代码
第二种:AsyncTask
Java代码
package com.cnjmwl.scm.salesorder;
/**
* 使用AsyncTask进行分页
*/
import java.util.ArrayList;
import com.cnjmwl.scm.MainActivity;
import com.cnjmwl.scm.R;
import com.cnjmwl.util.HttpUtil;
import com.cnjmwl.util.ReadXML;
import com.cnjmwl.util.StringUtil;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout.LayoutParams;
import com.cnjmwl.scm.entity.*;
public class SalesOrderActivity extends Activity implements OnScrollListener{
private ListView mListView;
private MyAdapter mMyAdapter;
LinearLayout loadingLayout;
private int lastItem=0;
/**
* 设置布局显示属性
*/
private LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
/**
* 设置布局显示目标最大化属性
*/
private LayoutParams FFlayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
private ProgressBar progressBar;
private boolean isLoading;
/**
* 报检属性名称
*/
String[] strID,strUnit,strStatus,strStatusDesc,strNO,strDate,strLinkMan,strLinkTel,business;
int maxCount;//数据库中的订单总数
ArrayList<SalesOrder> lists;
View.OnClickListener cl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order_main);
setupViews();
}
public void setupViews(){
if(maxCount==0){
addDeclaration();
}
//线性布局
LinearLayout layout=new LinearLayout(this);
//设置布局水平方向
layout.setOrientation(LinearLayout.HORIZONTAL);
//进度条
progressBar=new ProgressBar(this);
//把进度条加入到layout中
layout.addView(progressBar,mLayoutParams);
//文本内容
TextView textView=new TextView(this);
textView.setText("加载中...");
textView.setGravity(Gravity.CENTER_VERTICAL);
//把文本加入到layout中
layout.addView(textView,FFlayoutParams);
//设置layout的重力方向,即对齐方式是
layout.setGravity(Gravity.CENTER);
//设置ListView的页脚layout
loadingLayout = new LinearLayout(this);
loadingLayout.addView(layout,mLayoutParams);
loadingLayout.setGravity(Gravity.CENTER);
//得到一个ListView用来显示条目
mListView=(ListView)findViewById(R.id.listview);
//添加到页脚
mListView.addFooterView(loadingLayout);
mMyAdapter=new MyAdapter();
mListView.setAdapter(mMyAdapter);
mListView.setOnScrollListener(this);
mListView.setTextFilterEnabled(true);
mListView.setBackgroundResource(R.drawable.background);
}
/**
* 向服务器请求待办列表
*/
private String queryList(String uri){
String url = HttpUtil.BASE_URL+uri;
System.out.println(url);
return HttpUtil.getHTTPUtil().queryStringForPost(url);
}
public void addDeclaration(){
String result2=queryList("orderStatusList".trim());
lists=ReadXML.readXML(result2).getInspection2();
Log.v("result2--", result2);
strID=new String[lists.size()];
strNO=new String[lists.size()];
strStatus=new String[lists.size()];
strStatusDesc=new String[lists.size()];
strUnit=new String[lists.size()];
strDate=new String[lists.size()];
strLinkMan=new String[lists.size()];
strLinkTel=new String[lists.size()];
business=new String[lists.size()];
int i2=0;
for(SalesOrder in:lists){
strID[i2]=in.getId();
strStatus[i2]=in.getStatus();
strDate[i2]=in.getDate();
strLinkTel[i2]=in.getLinkTel();
//business[i2]=business[i2].replace("@", "\n");
if(in.getStatusDesc()!=null&&!in.getStatusDesc().equals("")){
strStatusDesc[i2]=StringUtil.unicodeToUtf8(in.getStatusDesc());
}else {
strStatusDesc[i2]="";
}
if(in.getUnit()!=null&&!in.getUnit().equals("")){
strUnit[i2]=StringUtil.unicodeToUtf8(in.getUnit());
}else {
strUnit[i2]="";
}
if(in.getLinkMan()!=null&&!in.getLinkMan().equals("")){
strLinkMan[i2]=StringUtil.unicodeToUtf8(in.getLinkMan());
}else {
strLinkMan[i2]="";
}
if(in.getBusinessItems()!=null&& !in.getBusinessItems().equals("")){
business[i2]=StringUtil.unicodeToUtf8(in.getBusinessItems());
business[i2]=business[i2].replace("||", "\n");
}else {
business[i2]="";
}
//business[i2]=business[i2].replace("|", "\n");
Log.v("business", business[i2]);
strNO[i2]=in.getNo();
i2++;
}
maxCount=strID.length;
}
/**
* 创建菜单
*
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, Menu.FIRST+1, 5, "返回首页").setIcon(android.R.drawable.ic_menu_delete);
menu.add(Menu.NONE, Menu.FIRST+2, 2, "退出").setIcon(android.R.drawable.ic_menu_edit);
// this.getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
//菜单项被选择事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST + 1:
Intent intent=new Intent(SalesOrderActivity.this,MainActivity.class);
startActivity(intent);
break;
case Menu.FIRST + 2:
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
System.exit(0);
break;
}
return false;
}
//定义自己的适配器,注意getCount和getView方法
private class MyAdapter extends BaseAdapter{
int count = 10 ;
@Override
public int getCount() {
return count;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_data, null);
TextView noTextView=(TextView)convertView.findViewById(R.id.ItemNO);
noTextView.setText(strNO[position]);
TextView LinkManTextView=(TextView)convertView.findViewById(R.id.ItemLinkMan);
LinkManTextView.setText(strLinkMan[position]);
TextView LinkTelTextView=(TextView)convertView.findViewById(R.id.ItemLinkTel);
LinkTelTextView.setText(strLinkTel[position]);
TextView busTextView=(TextView)convertView.findViewById(R.id.Itembusiness1);
busTextView.setText(business[position]);
return convertView;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Log.i("test", "Scroll>>>first: " + firstVisibleItem
+ ", visible: " + visibleItemCount + ", total: "
+ totalItemCount);
lastItem = firstVisibleItem + visibleItemCount -1;
Log.i("test", "Scroll>>>lastItem:" + lastItem);
Log.i("mMyAdapter.count", mMyAdapter.count+"");
Log.i("maxCount", maxCount+"");
if(mMyAdapter.count < maxCount){
if(firstVisibleItem + visibleItemCount == totalItemCount){
//isLoading用于控制重复加载
if(!isLoading){
new MyTask().execute(null);
}
}
}else {
mListView.removeFooterView(loadingLayout);
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
private class MyTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
isLoading =true;
}
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(mMyAdapter.count < maxCount){
if((maxCount - mMyAdapter.count) >10){
mMyAdapter.count+=10;
}else {
mMyAdapter.count+= maxCount -mMyAdapter.count;
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
mMyAdapter.notifyDataSetChanged();
mListView.setSelection(lastItem);
int currentPage = mMyAdapter.count / 10;
Toast.makeText(getApplicationContext(), "第" + currentPage + "页",
Toast.LENGTH_LONG).show();
isLoading = false;
}
}
}
复制代码 |
|