今天分享一个Android ListView滚动进度加载的实例

时间:2022-09-20 19:23:44
今天分享一个Android ListView滚动进度加载的实例,在实际应用中经常需要用到. 这里的数据为本地模拟. 当数据被完全加载,停止加载,并移除进度条. 如果数据来源于网络,记得加*问网络的权限。       
 
     下面是完整代码.  欢迎批评指正。
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AbsListView.OnScrollListener;
 
 
public class TestActivity extends Activity {
 
  SimpleAdapter sa;
  ListView list; View loadingView;
  List<Map<String, Object>> arraylist = new ArrayList<Map<String, Object>>();
  //boolean flag=true; int count;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  
  this.setContentView(R.layout.data_list);
  
  //String data = this.getIntent().getStringExtra("result");
  
  list = (ListView)this.findViewById(R.id.list);
  loadingView = this.getLayoutInflater().inflate(R.layout.loading, null);
  list.addFooterView(loadingView);
  
  
  sa = new SimpleAdapter(this,arraylist,R.layout.item,
                new String[]{"id","title","star"},
                new int[]{R.id.id,R.id.title,R.id.star});
  
  list.setAdapter(sa);
  
  list.setOnScrollListener(new OnScrollListener(){
   @Override
   public void onScroll(AbsListView view, int firstVisibleItem,
     int visibleItemCount, int totalItemCount) {
    // TODO Auto-generated method stub
    
    if(firstVisibleItem + visibleItemCount == totalItemCount){
     
     //测试请求100条数据,如果超出则不用请求
     if(list.getCount()<=100){
        Message msg = new Message();
           msg.what = 0;
           mHandler.sendMessage(msg);
     }else{
      Message msg = new Message();
            msg.what = 2;
            mHandler.sendMessage(msg);
     }
     
     
       }
   }
   @Override
   public void onScrollStateChanged(AbsListView view, int scrollState) {
    // TODO Auto-generated method stub
     
   }
            
  });
  
 }
    
 
 
 private void friendsList() {
       
        // 测试数据
  for(int i=0;i<15;i++) {
      Map<String, Object> map = new HashMap<String, Object>();
          map.put("id", "33355124");
          map.put("star", "********");
          map.put("title", "张山来也..");
          arraylist.add(map);
  }
    
 }
 
 private Handler mHandler = new Handler(){
  @Override
  public void handleMessage(Message msg) {
   // TODO Auto-generated method stub
   super.handleMessage(msg);
   switch(msg.what){
   case 0:{
    
    sa.notifyDataSetChanged();
    Message m = new Message();
    m.what = 1;
    mHandler2.sendMessage(m);
    break;
   }
   case 2:{
    list.removeFooterView(loadingView);
    sa.notifyDataSetChanged();
    break;
   }
   
   }
  }
  
  };
 
  private Handler mHandler2 = new Handler(){
  @Override
  public void handleMessage(Message msg) {
   // TODO Auto-generated method stub
   super.handleMessage(msg);
   
   switch(msg.what){
   case 1:{
    TestActivity.this.friendsList();
    list.removeFooterView(loadingView);
    
    //这里起到延时的作用
    for(int i=0;i<10000000;i++){
     int j = i;
    }
    sa.notifyDataSetChanged();
       list.addFooterView(loadingView);
    break;
   }
   }
  }
  
  };
}