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"