ListView是android中最常用的控件之一。
在實際運用中往往會遇到一次性加載全部數據過多,需要分頁加載增加程序運行效率!
本demo是將更新的監聽放在listview的foot中,通過底部按鈕點擊觸發或者通過滑動事件監聽,當滑動到列表最底部的時候觸發。
有圖有真像:
列表初始狀態
觸發加載
加載後
部分代碼:
MainActivity
復制代碼 代碼如下:
private void initActivity() {
// TODO Auto-generated method stub
loadMoreView = getLayoutInflater().inflate(R.layout.loadmore, null);
loadmore_btn = (Button) loadMoreView.findViewById(R.id.loadmore_btn);
loadmore_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
doUpdate();
}
});
listView = (ListView) findViewById(R.id.listView);
listView.addFooterView(loadMoreView);
initAdapter();
listView.setAdapter(adapter);
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
int itemsLastIndex = adapter.getCount() - 1;
int lastIndex = itemsLastIndex + 1;
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
&& visibleLast == lastIndex) {
// 可在這添加滑到列表底的時候異步自動加載
// doUpdate();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
visibleCount = visibleItemCount;
visibleLast = firstVisibleItem + visibleItemCount - 1;
}
});
}
protected void doUpdate() {
// TODO Auto-generated method stub
loadmore_btn.setText("loading ...");
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
loadData();
adapter.notifyDataSetChanged();// 通知adapter數據變化
listView.setSelection(visibleLast - visibleCount + 1);
loadmore_btn.setText("Load More");
}
}, 2000);
}
private void loadData() {
// TODO Auto-generated method stub
int count = adapter.getCount();
for (int i = count; i < count + 10; i++) {
adapter.addItem(String.valueOf(i + 1));
}
}
private void initAdapter() {
// TODO Auto-generated method stub
List<String> items = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
items.add(String.valueOf(i + 1));
}
adapter = new ListAdapter(this, items);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
ListAdapter
復制代碼 代碼如下:
public ListAdapter(Context context, List<String> items) {
this.items = items;
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
}
TextView textView = (TextView) convertView
.findViewById(R.id.list_item_text);
textView.setText(items.get(position));
return convertView;
}
/**
* 添加列表項
* @param item
*/
public void addItem(String item){
items.add(item);
}
提醒一下:要下載android-support-v4.jar 這個第三方的jar包哦! 這裡就不提供下載了!