在android中最重要的常用组件之一ListView,几乎任何应用都有他们的影子。但我们每次在使用的时候总是忙于配置各种事件监听,特别是他的下拉刷新和加载更多。
前几天在github网站上看到一个比较好的开源框架。使用也是非常简单,我们只需要在onRefresh()和onLoadMore() 去加载我们的数据,他就会自动加到ListView中,
省去了很多麻烦,实现效果图
[java]
package me.maxwin;
import java.util.ArrayList;
import java.util.Random;
import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
public class XListViewActivity extends Activity implements IXListViewListener {
private XListView mListView;
private MyAdapter mAdapter;
private ArrayList<Person> items = new ArrayList<Person>();
private Handler mHandler;
private int start = 0;
private static int refreshCnt = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
geneItems();
mListView = (XListView) findViewById(R.id.xListView);
mListView.setPullLoadEnable(true);
mAdapter = new MyAdapter(this, items);
mListView.setAdapter(mAdapter);
// mListView.setPullLoadEnable(false);
// mListView.setPullRefreshEnable(false);
mListView.setXListViewListener(this);
mHandler = new Handler();
}
private void geneItems() {
Random r = new Random();
for (int i = 0; i != 20; ++i) {
items.add(new Person("qingxue " + (++start),"1358888"+r.nextInt(10)+r.nextInt(10)+r.nextInt(10)+r.nextInt(10)));
}
}
private void onLoad() {
mListView.stopRefresh();
mListView.stopLoadMore();
mListView.setRefreshTime("刚刚");
}
//下拉刷新
@Override
public void onRefresh() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
start = ++refreshCnt;
items.clear();
geneItems();
// mAdapter.notifyDataSetChanged();
//mAdapter = new ArrayAdapter<String>(XListViewActivity.this, R.layout.list_item, items);
//mListView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
onLoad();
}
}, 2000);
}
//加载更多
@Override
public void onLoadMore() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
geneItems();
mAdapter.notifyDataSetChanged();
onLoad();
}
}, 2000);
}
}
另外还有适配器,这个必须自己写的,要看你生成什么样的条目了,我只是为了演示一个简单的实现
[java]
package me.maxwin;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<Person> list;
public MyAdapter(Context context){
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.list = new ArrayList<Person>();
}
public MyAdapter(Context context,List<Person> list){
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override &nbs
补充:移动开发 , Android ,