Android中使用Listview動態加載數據

listview 是android網絡開發中經常使用的一種控件,像現在的人人網、微博等等android客戶端,都在使用它,但是大家不難發現其在加載數據的過程中不是一次性加載完,而是沒等你看過固定條數據後再繼續加載,這樣對於需要加載的大量數據;來說分開進行,客戶體驗比較好。一下是listview實現動態加載數據的方法。只是一個簡單的框架。(轉自於網絡)

import android.app.Activity;
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.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout.LayoutParams;
public class ListViewForLoading extends Activity implements OnScrollListener {
private listViewAdapter adapter = new listViewAdapter();
ListView listView;
LinearLayout loadingLayout;
private Thread mThread;
/**
* 設置佈局顯示屬性
*/
private LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
/**
* 設置佈局顯示目標最大化屬性
*/
private LayoutParams FFlayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
private ProgressBar progressBar;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
// TODO Auto-generated method stub
// 線性佈局
LinearLayout layout = new LinearLayout(this);
// 設置佈局 水平方向
layout.setOrientation(LinearLayout.HORIZONTAL);
// 進度條
progressBar = new ProgressBar(this);
// 進度條顯示位置
progressBar.setPadding(0, 0, 15, 0);
// 把進度條加入到layout中
layout.addView(progressBar, mLayoutParams);
// 文本內容
TextView textView = new TextView(this);
textView.setText("加載中...");
textView.setGravity(Gravity.CENTER_VERTICAL);
// 把文本加入到layout中
layout.addView(textView, FFlayoutParams);
// 設置layout的重力方向,即對齊方式是
layout.setGravity(Gravity.CENTER);
// 設置ListView的頁腳layout
loadingLayout = new LinearLayout(this);
loadingLayout.addView(layout, mLayoutParams);
loadingLayout.setGravity(Gravity.CENTER);
// 得到一個ListView用來顯示條目
listView = (ListView) findViewById(R.id.tv);
// 添加到腳頁顯示
listView.addFooterView(loadingLayout);
// 給ListView添加適配器
listView.setAdapter(adapter);
// 給ListView註冊滾動監聽
listView.setOnScrollListener(this);
}
/**
* 要用用於生成顯示數據
*
* @author huangbq
*
*/
class listViewAdapter extends BaseAdapter {
int count = 10;
public int getCount() {
return count;
}
public Object getItem(int pos) {
return pos;
}
public long getItemId(int pos) {
return pos;
}
public View getView(int pos, View v, ViewGroup p) {
TextView view;
if (v == null) {
view = new TextView(ListViewForLoading.this);
} else {
view = (TextView) v;
}
view.setText("ListItem " + pos);
view.setTextSize(20f);
view.setGravity(Gravity.CENTER);
view.setHeight(60);
return view;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
if(firstVisibleItem+visibleItemCount==totalItemCount)
{
//開線程去下載網絡數據
if (mThread == null || !mThread.isAlive()) {
mThread = new Thread() {
@Override
public void run() {
try {
//這裏放你網絡數據請求的方法,我在這裏用線程休眠5秒方法來處理
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
mThread.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 (adapter.count <= 41) {
adapter.count += 10;
int currentPage = adapter.count / 10;
Toast.makeText(getApplicationContext(),"第" + currentPage + "頁", Toast.LENGTH_LONG).show();
} else {
listView.removeFooterView(loadingLayout);
}
//重新刷新Listview的adapter裏面數據
adapter.notifyDataSetChanged();
break;
default:
break;
}
}
};
}

程序運行結果:


 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章