安卓框架——XListView(上拉加载,下拉刷新)的使用方法
Xlistview项目主要是三部分:XlistView,XListViewHeader,XListViewFooter,分别是XListView主体、Header、Footer的实现。Header是通过设置height,Footer是通过设置BottomMargin来模拟拉伸效果。
实现IXListViewListener接口中的onRefresh()和onLoadMore()方法。每个方法中还需要调用onLoad()方法,关闭刷新和加载。
使用方法:
1.获取XListView控件。
2.上拉刷新setPullLoadEnable(true)。
3.添加数据,适配器。
4.给xListView设置监听setXListViewListener(this)。
5.实现onRefresh()和onLoadMore()方法。
6.调用onLoad()关闭刷新和加载。
7.layout中必须有header.xml和footer.xml。页面中加入XListView控件。
- <me.maxwin.view.XListView
- android:id="@+id/xListView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:cacheColorHint="#00000000" >
- handler =new Handler();
- arrayAddList();// 添加数据
- xListView = (XListView)findViewById(R.id.xListView);//获取XListView控件
- xListView.setPullLoadEnable(true);//上拉刷新
- // xListView.setPullRefreshEnable(true);//下拉刷新(可以不设)
- arrayAdapter = new ArrayAdapter<String>(this, R.layout.listview_item,arrayList);//列表适配器
- xListView.setAdapter(arrayAdapter);//指定adapter
- xListView.setXListViewListener(this);//给xListView设置监听
- // 刷新
- @Override
- public void onRefresh() {
- handler.postDelayed(new Runnable() {
- @Override
- public void run() {
- arrayList.add("XListView刷新==" + (++start));
- // 如果适配器的内容改变时需要强制调用getView来刷新每个Item
- arrayAdapter.notifyDataSetInvalidated();
- onLoad();// 必须调用此方法,结束加载状态
- }
- }, 2000);
- }
- // 加载更多
- @Override
- public void onLoadMore() {
- handler.postDelayed(new Runnable() {
- @Override
- public void run() {
- arrayList.add("XListView刷新==" + (++start));
- // 如果适配器的内容改变时需要强制调用getView来刷新每个Item
- arrayAdapter.notifyDataSetChanged();
- onLoad();// 必须调用此方法,结束加载状态
- }
- }, 2000);
- }
- // 获得数据后一定要调用onLoad()方法,否则刷新会一直进行,根本停不下来
- private void onLoad() {
- xListView.stopRefresh();//停止刷新
- xListView.stopLoadMore();//停止加载更多
- SimpleDateFormat formatter = new SimpleDateFormat("MM-ddHH:mm:ss");//设置日期显示格式
- Date curDate = new Date(System.currentTimeMillis());//获取当前时间
- String str = formatter.format(curDate);// 将时间装换为设置好的格式
- xListView.setRefreshTime(str);//设置时间
- }
注意事项:
1.给XListViewListener设置监听事件。ListView.setXListViewListener(this)。
2.获取数据后调用onLoad()方法。
3.背景颜色设置为透明android:cacheColorHint="#00000000"

浙公网安备 33010602011771号