listview底部加载更多的操作

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();
				}
			}
		}
	}









  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值