1,布局文件
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:background="@drawable/listfoot_btn_default"
>
<Button
android:id="@+id/btn_foot_more"
android:layout_width="fill_parent"
android:layout_height="@dimen/listitemheight"
android:background="@drawable/button_more_info"
android:gravity="center"
android:text="加载更多"
android:textColor="@color/font_color"
android:textSize="16.0sp" />
<LinearLayout
android:id="@+id/foot_loading"
android:layout_width="fill_parent"
android:layout_height="@dimen/listitemheight"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone"
>
<ProgressBar
android:id="@+id/footprogress"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:indeterminateBehavior="repeat" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center"
android:padding="3.0px"
android:text="加载中..."
android:textColor="@color/font_color" />
</LinearLayout>
</LinearLayout>
2.分页及主要代码
int pageNow = 1;
int totalPage;
ListView listview;
LinearLayout loading;
LinearLayout footview;
Button btnmore;
ProgressDialog dialog;
TextView txtNodata;
ListBaseAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.type);
init();
initFootview();
new GetListInfo().execute();
}
//初始化基本信息
private void init(){
dialog = new ProgressDialog(this);
dialog.setMessage("数据加载中...");
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
ApkInfo apk = (ApkInfo)parent.getItemAtPosition(position);
Intent intent = new Intent(NewRank.this,ApkDownload.class);
intent.putExtra("apkinfo",apk);
startActivity(intent);
}
});
}
//初始化底部信息
private void initFootview(){
footview = (LinearLayout)LayoutInflater.from(NewRank.this).inflate(R.layout.list_foot, null);
loading = (LinearLayout)footview.findViewById(R.id.foot_loading);
btnmore = (Button)footview.findViewById(R.id.btn_foot_more);
btnmore.setOnClickListener(new OnClickListener() { //加载更多的响应事件
public void onClick(View v) {
// TODO Auto-generated method stub
btnmore.setVisibility(View.GONE);
loading.setVisibility(View.VISIBLE);
pageNow++;
new GetListInfo().execute();
}
});
}
//获取信息
class GetListInfo extends AsyncTask<Void, Integer,List<ApkInfo>>{
@Override
protected void onPreExecute() {
if(pageNow == 1){
dialog.show();
}
}
@Override
protected List<ApkInfo> doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
//return new FastRankBus().getApkInfosByPage(pageNow, ""); //调用相关的逻辑获得数据
} catch (Exception e) {
publishProgress(1);
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
if(values[0] ==1){
showMessage("获取数据出错");
dialog.dismiss();
txtNodata.setVisibility(View.VISIBLE);
}
}
@Override
protected void onPostExecute(List<ApkInfo> result) {
// TODO Auto-generated method stub
if(result == null){
txtNodata.setVisibility(View.VISIBLE);
}else{
//有数据
if(pageNow == 1){ //首次加载S
adapter = new FastRankAdapter(result, NewRank.this);
if(totalPage > pageNow){
listview.addFooterView(footview);
btnmore.setVisibility(View.VISIBLE);
loading.setVisibility(View.GONE);
}
listview.setAdapter(adapter);
dialog.dismiss();
}else{ //加载更多获取数据
if(pageNow == totalPage){ //最后一页
listview.removeFooterView(footview);
}
adapter.updateData(result); //追加追加地步信息
adapter.notifyDataSetChanged();
}
}
}
}