重写了listiview,在怎么做一个分页 的效果。上拉可以显示更多数据
/**
* 这个方法是根据结果的大小来决定footer显示的。
* <p>
* 这里假定每次请求的条数为10。如果请求到了10条。则认为还有数据。如过结果不足10条,则认为数据已经全部加载,这时footer显示已经全部加载
* </p>
*
* @param resultSize
*/
public void setResultSize(int resultSize) {
if (resultSize == 0) {
isLoadFull = true;
loadFull.setVisibility(View.GONE);
loading.setVisibility(View.GONE);
more.setVisibility(View.GONE);
noData.setVisibility(View.VISIBLE);
} else if (resultSize > 0 && resultSize < pageSize) {
isLoadFull = true;
loadFull.setVisibility(View.VISIBLE);
loading.setVisibility(View.GONE);
more.setVisibility(View.GONE);
noData.setVisibility(View.GONE);
} else if (resultSize == pageSize) {
isLoadFull = false;
loadFull.setVisibility(View.GONE);
loading.setVisibility(View.VISIBLE);
more.setVisibility(View.VISIBLE);
noData.setVisibility(View.GONE);
}
}
package com.flea;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import com.flea.autolistview.widget.AutoListView;
import com.flea.autolistview.widget.AutoListView.OnLoadListener;
import com.flea.autolistview.widget.AutoListView.OnRefreshListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity implements OnRefreshListener,
OnLoadListener {
private ArrayList<HashMap<String, Object>> resultListForJson=new ArrayList<HashMap<String,Object>>();
private ArrayList<HashMap<String, Object>> resultList=new ArrayList<HashMap<String,Object>>();
private ImageButton imgbtnnewdata;
private AutoListView lstv;
private MyAdapter myadapter;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
ArrayList<HashMap<String, Object>> result = (ArrayList<HashMap<String, Object>>) msg.obj;
switch (msg.what) {
case AutoListView.REFRESH:
lstv.onRefreshComplete();
resultList.clear();
resultList.addAll(result);
break;
case AutoListView.LOAD:
lstv.onLoadComplete();
resultList.addAll(result);
break;
}
Log.d("sqt", "resultList"+resultList);
lstv.setResultSize(resultListForJson.size());
myadapter.notifyDataSetChanged();
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstv = (AutoListView) findViewById(R.id.lstv);
//upData();//更新listview数据
Log.d("sqt", "resultList:"+resultList);
myadapter = new MyAdapter(MainActivity.this, resultList);
lstv.setAdapter(myadapter);
lstv.setOnRefreshListener(MainActivity.this);
lstv.setOnLoadListener(MainActivity.this);
initData();
Log.d("sqt","start upData");
upData();
Log.d("sqt","stop upData");
//final ListView myListView=(ListView)findViewById(R.id.listmenu);
imgbtnnewdata=(ImageButton)findViewById(R.id.imageButton1);
//需要在layout中创建listview,adpate将自动使用listview,其实listview的id名字需要为原始id,android:id="@+id/android:list"
//upData();//更新listview数据
setOnClickListener();//按钮的动作
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void upData(){
String url="http://192.168.1.128:8080/JsonServerHttp/GetSalesServlet";
HttpAsyncTask getMyString=new HttpAsyncTask(MainActivity.this);
resultListForJson=getMyString.doInBackground(url);
Log.d("sqt", "resultListForJson:"+resultListForJson);
}
public void setOnClickListener(){
imgbtnnewdata.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
private void initData() {
loadData(AutoListView.REFRESH);
}
private void loadData(final int what) {
// 这里模拟从服务器获取数据
new Thread(new Runnable() {
upData();
@Override
public void run() {
try {
Thread.sleep(700);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg = handler.obtainMessage();
msg.what = what;
msg.obj = getData();
handler.sendMessage(msg);
}
}).start();
}
@Override
public void onLoad() {
// TODO Auto-generated method stub
loadData(AutoListView.LOAD);
}
@Override
public void onRefresh() {
// TODO Auto-generated method stub
loadData(AutoListView.REFRESH);
}
// 测试数据
public ArrayList<HashMap<String, Object>> getData() {
ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
//upData();
Log.d("sqt","start");
String salename=resultListForJson.get(1).get("salename").toString();
System.out.println("salename1"+salename);
for (int i=(resultListForJson.size()-1);i>-1;i--) {
HashMap<String, Object> map=new HashMap<String, Object>();
map.put("salename",resultListForJson.get(i).get("salename").toString());
map.put("saletime",resultListForJson.get(i).get("saletime").toString());
map.put("salemoney",resultListForJson.get(i).get("salemoney").toString());
result.add(map);
Log.d("sqt", "result:"+result);
}
return result;
}
}