listview分页加载 ListView 分页加载更新实例分享
人气:0想了解ListView 分页加载更新实例讲解的相关内容吗,在本文为您仔细讲解listview分页加载的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:listview,分页,加载,下面大家一起来学习吧。
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包哦! 这里就不提供下载了!
加载全部内容