Android ListView 的动态加载数据

openkk 13年前

package com.src.hero;    import android.app.Activity;  import android.content.Context;  import android.os.Bundle;  import android.os.Handler;  import android.os.Message;  import android.view.Gravity;  import android.view.View;  import android.view.ViewGroup;  import android.view.ViewGroup.LayoutParams;  import android.widget.AbsListView;  import android.widget.AbsListView.OnScrollListener;  import android.widget.BaseAdapter;  import android.widget.LinearLayout;  import android.widget.ListView;  import android.widget.ProgressBar;  import android.widget.TextView;  import android.widget.Toast;  /**   * @author Hero   * 2012-3-8 下午11:04:21   * 实现动态加载的效果   */  public class DynamicLoadActivity extends Activity {   private ListView lv_main;   private LinearLayout linearlayout = null;   private LayoutParams wlayoutparams, flayoutparams;   listAdapter adpter = null;   Thread thread;     /** Called when the activity is first created. */   @Override   public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    lv_main = (ListView) findViewById(R.id.listView1);    adpter = new listAdapter(this);    wlayoutparams = new LinearLayout.LayoutParams(320, 50);// 定义的是FootView的宽高    flayoutparams = new LinearLayout.LayoutParams(      LinearLayout.LayoutParams.WRAP_CONTENT,      LinearLayout.LayoutParams.WRAP_CONTENT);// 定义FootView中控件的布局    ProgressBar progressbar = new ProgressBar(this);// 定义一个ProgressBar    progressbar.setPadding(50, 0, 0, 0);// left, top, right, bottom    TextView tv_wait = new TextView(this);// 定义一个TextView    tv_wait.setText("正在动态加载……");    tv_wait.setTextSize(20f);    tv_wait.setGravity(Gravity.CENTER_VERTICAL);    tv_wait.setPadding(10, 0, 0, 0);    LinearLayout layout = new LinearLayout(this);    layout.setOrientation(LinearLayout.HORIZONTAL);    layout.addView(progressbar, flayoutparams);    layout.addView(tv_wait, flayoutparams);    layout.setGravity(Gravity.CENTER);    linearlayout = new LinearLayout(this);    linearlayout.addView(layout, wlayoutparams);    linearlayout.setGravity(Gravity.CENTER_VERTICAL);    lv_main.addFooterView(linearlayout);    lv_main.setAdapter(adpter);    lv_main.setOnScrollListener(scroller);   }      /**       * 滚动监听器       */   OnScrollListener scroller = new OnScrollListener() {    @Override    public void onScroll(AbsListView view, int firstVisibleItem,      int visibleItemCount, int totalItemCount) {     // TODO Auto-generated method stub     if (firstVisibleItem + visibleItemCount == totalItemCount) {      // 启动一个线程去加载网络数据      if (thread == null || !thread.isAlive()) {       thread = new Thread() {        @Override        public void run() {         try {          // 处理网络方法,在这里用线程休眠3秒方法来处理          Thread.sleep(3000);         } catch (InterruptedException e) {          e.printStackTrace();         }         Message message = new Message();         message.what = 1;         handler.sendMessage(message);        }       };       thread.start();      }     }    }      @Override    public void onScrollStateChanged(AbsListView view, int scrollState) {     // TODO Auto-generated method stub      }     };      /**       * 消息队列       */   private Handler handler = new Handler() {    @Override    public void handleMessage(Message msg) {     // TODO Auto-generated method stub     switch (msg.what) {     case 1:      if (adpter.count <= 101) {       adpter.count += 10;       int currentPage = adpter.count / 10;       Toast.makeText(getApplicationContext(),         "第" + currentPage + "页", Toast.LENGTH_LONG).show();      } else {       lv_main.removeFooterView(linearlayout);      }      // 重新刷新Listview的adapter里面数据      adpter.notifyDataSetChanged();      break;     default:      break;     }    }   };     public class listAdapter extends BaseAdapter {      int count = 10;    Context context;      public listAdapter(Context context) {     this.context = context;    }      @Override    public int getCount() {     // TODO Auto-generated method stub     return count;    }      @Override    public Object getItem(int position) {     // TODO Auto-generated method stub     return position;    }      @Override    public long getItemId(int item) {     // TODO Auto-generated method stub     return item;    }      @Override    public View getView(int position, View convertView, ViewGroup parent) {     // TODO Auto-generated method stub     TextView tv_content;     if (convertView == null) {      tv_content = new TextView(context);     } else {      tv_content = (TextView) convertView;     }     tv_content.setText("Item     " + position);     tv_content.setTextSize(20f);//字体的大小20f     tv_content.setGravity(Gravity.CENTER);//显示在中间     tv_content.setHeight(50);//高度为50     return tv_content;    }     }  }
Android ListView 的动态加载效果 Android ListView 的动态加载效果