* 问题描述:
PullToRefresh 上拉刷新的时候向下滑动,这时候可能会报IndexOutOfBoundsException;
本PullToRefresh 针对所有第三方下拉刷新上拉加载更多的控件
PullToRefresh设置监听:
//设置刷新监听
mPullToRefreshListView.setOnRefreshListener(mListViewOnRefreshListener2);
private PullToRefreshBase.OnRefreshListener2<ListView> mListViewOnRefreshListener2 = new PullToRefreshBase.OnRefreshListener2<ListView>() {
/**
* 下拉刷新回调
* @param refreshView
*/
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
//模拟延时三秒刷新
mPullToRefreshListView.postDelayed(new Runnable() {
@Override
public void run() {
mItems.clear();
mockList(0, 20);
mArrayAdapter.notifyDataSetChanged();
mPullToRefreshListView.onRefreshComplete();//下拉刷新结束,下拉刷新头复位
}
}, 3000);
}
/**
* 上拉加载更多回调
* @param refreshView
*/
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
//模拟延时三秒加载更多数据
mPullToRefreshListView.postDelayed(new Runnable() {
@Override
public void run() {
mockList(mItems.size(), 20);
mArrayAdapter.notifyDataSetChanged();
mPullToRefreshListView.onRefreshComplete();//上拉加载更多结束,上拉加载头复位
}
}, 3000);
}
};
* 问题解决:
mItems.clear();放在网络加载完成之后;
主要原因是因为:mItems清空之后,由于网络加载没有完成,但是Listview仍然有可能被滑动,而adapter的getView获取不到数据,则会报IndexOutOfBoundsException