1 布局定义
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/lv_per_seek_result"
android:layout_width="match_parent"
android:dividerHeight="5dp"
android:layout_height="match_parent"/>
2 设置(初始化)
/**
* 初始化 pullToRefresh
*/
private void initListView() {
lvPerLife = (PullToRefreshListView) findViewById(R.id.lv_per_seek_result);
ILoadingLayout startLabels = lvPerLife.getLoadingLayoutProxy(true, false);
startLabels.setPullLabel("下拉刷新...");// 刚下拉时,显示的提示
startLabels.setRefreshingLabel("正在载入...");// 刷新时
startLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时,显示的提示
ILoadingLayout endLabels = lvPerLife.getLoadingLayoutProxy(
false, true);
endLabels.setPullLabel("上拉刷新...");// 刚下拉时,显示的提示
endLabels.setRefreshingLabel("正在载入...");// 刷新时
endLabels.setReleaseLabel("松开加载...");// 下来达到一定距离时,显示的提示
lvPerLife.setOnRefreshListener(new MyOnRefreshListener());
//底部监听
lvPerLife.setOnLastItemVisibleListener(new MyOnLastItemVisibleListener());
lvPerLife.setMode(PullToRefreshBase.Mode.BOTH); //上下都可以刷新
lvPerLife.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
do something
//adaper.getItem(position-1)
//position 要减一,因为里面有个view了
//如果有调用addHeaderView(View); 则添加一个 position 多减一个位置
}
});
adaper = new PerLifeAdaper(this);
lvPerLife.setAdapter(adaper);
}
3 监听
private class MyOnRefreshListener implements PullToRefreshBase.OnRefreshListener2<ListView> {
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
//上推到顶 重置第一页(开始的数据) 清空之前的数据
//pageIndex=1; // isClean=true;
} @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { if ( pageIndex*pageSize< totalNumber) { //未拿完数据时 ++pageIndex; //获取数据 } else {
//没有数据,延迟关闭刷新
handler.sendEmptyMessageDelayed(101,1000);
}
}
}
private class MyOnLastItemVisibleListener implements PullToRefreshBase.OnLastItemVisibleListener {
@Override
public void onLastItemVisible() {
}
}
case 101: //没有数据了
lvPerLife.onRefreshComplete();
Toast.makeText(PerSeekResultActivity.this, "没有更多数据了", Toast.LENGTH_SHORT).show();
break;