安卓框架——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控件。

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <me.maxwin.view.XListView  
  2.      android:id="@+id/xListView"  
  3.      android:layout_width="fill_parent"  
  4.      android:layout_height="fill_parent"  
  5.      android:cacheColorHint="#00000000" >  

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. handler =new Handler();  
  2.         arrayAddList();// 添加数据  
  3.         xListView = (XListView)findViewById(R.id.xListView);//获取XListView控件  
  4.         xListView.setPullLoadEnable(true);//上拉刷新  
  5.         // xListView.setPullRefreshEnable(true);//下拉刷新(可以不设)  
  6.         arrayAdapter = new ArrayAdapter<String>(this, R.layout.listview_item,arrayList);//列表适配器  
  7.         xListView.setAdapter(arrayAdapter);//指定adapter  
  8.         xListView.setXListViewListener(this);//给xListView设置监听  
  9. // 刷新  
  10.     @Override  
  11.     public void onRefresh() {  
  12.         handler.postDelayed(new Runnable() {  
  13.             @Override  
  14.             public void run() {  
  15.                 arrayList.add("XListView刷新==" + (++start));  
  16.                 // 如果适配器的内容改变时需要强制调用getView来刷新每个Item  
  17.                 arrayAdapter.notifyDataSetInvalidated();  
  18.                 onLoad();// 必须调用此方法,结束加载状态  
  19.             }  
  20.         }, 2000);  
  21.     }  
  22.    
  23.     // 加载更多  
  24.     @Override  
  25.     public void onLoadMore() {  
  26.         handler.postDelayed(new Runnable() {  
  27.             @Override  
  28.             public void run() {  
  29.                 arrayList.add("XListView刷新==" + (++start));  
  30.                 // 如果适配器的内容改变时需要强制调用getView来刷新每个Item  
  31.                 arrayAdapter.notifyDataSetChanged();  
  32.                 onLoad();// 必须调用此方法,结束加载状态  
  33.             }  
  34.         }, 2000);  
  35.     }  
  36.    
  37.     // 获得数据后一定要调用onLoad()方法,否则刷新会一直进行,根本停不下来  
  38.     private void onLoad() {  
  39.         xListView.stopRefresh();//停止刷新  
  40.         xListView.stopLoadMore();//停止加载更多  
  41.         SimpleDateFormat formatter = new SimpleDateFormat("MM-ddHH:mm:ss");//设置日期显示格式  
  42.         Date curDate = new Date(System.currentTimeMillis());//获取当前时间  
  43.         String str = formatter.format(curDate);// 将时间装换为设置好的格式  
  44.         xListView.setRefreshTime(str);//设置时间  
  45.     }  

 

   

注意事项:
1.给XListViewListener设置监听事件。ListView.setXListViewListener(this)。
2.获取数据后调用onLoad()方法。

3.背景颜色设置为透明android:cacheColorHint="#00000000"

posted @ 2016-12-30 15:59  天涯海角路  阅读(216)  评论(0)    收藏  举报