PageAndPullToRefresh工具包
分页加载和下拉刷新,几行代码完全搞定
具体实现方法如下:
实例化一个PageAndRefreshListView,
可以通过layout来加载
<com.king.refresh.widget.PageAndRefreshListView
android:id="@+id/balance_page_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fadingEdge="none"
android:fadingEdgeLength="0px"
android:scrollbars="none"
android:divider="@color/grey"
android:dividerHeight="0.5dip"
refresh:pageDemandingEnable="true"
android:overScrollMode="never"
refresh:refreshable="true"
android:scrollingCache="false" />
mListView = (PageAndRefreshListView) view.findViewById(R.id.balance_page_list);
给ListView设置adapter ,这个adapter需要继承PageAndRefreshBaseAdapter
示例如下:
adapter = new BalanceInfoAdapter(mContext, new BalanceDataReuqest(mContext, map));
mListView.setAdapter(adapter);
adapter 示例:
public class BalanceInfoAdapter extends PageAndRefreshBaseAdapter {
private LayoutInflater mInflater;
public BalanceInfoAdapter(Context context,
PageAndRefreshRequestService requestService) {
super(requestService);
mInflater = LayoutInflater.from(context);
}
class ViewHolder {
public TextView bl_time_tv;
public TextView bl_comefrom_tv;
public TextView bl_income_tv;
public TextView bl_sumcount_tv;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.balance_list_item, null);
holder.bl_time_tv = (TextView) convertView
.findViewById(R.id.bl_time_tv);
holder.bl_comefrom_tv = (TextView) convertView
.findViewById(R.id.bl_comefrom_tv);
holder.bl_income_tv = (TextView) convertView
.findViewById(R.id.bl_income_tv);
holder.bl_sumcount_tv = (TextView) convertView
.findViewById(R.id.bl_sumcount_tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
BalanceInfo data = (BalanceInfo) getItem(position);
if (data != null) {
holder.bl_time_tv.setText(data.getCreateTime());
holder.bl_comefrom_tv.setText(data.getPaymentTypeName());
holder.bl_income_tv.setText(data.getCount());
holder.bl_sumcount_tv.setText(data.getLeftCount());
}
return convertView;
}
}
实例化 PageAndRefreshBaseAdapter需要传递一个实现了PageAndRefreshRequestService接口的对象,该对象实现数据的获取与分页刷新机制,具体代码如下:
public class BalanceDataReuqest implements PageAndRefreshRequestService {
private PageAndRefreshRequestCallBack mCallBack;
private int page;
private Context mContext;
private Map<String, String> map = new HashMap<String, String>();
private Handler handler = new Handler() {
@SuppressWarnings("unchecked")
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
// 模拟请求结果,一次成功,一次失败,循环
case 1:
mCallBack.onRequestComplete((List<BalanceInfo>) msg.obj,
msg.arg1);
break;
default:
break;
}
super.handleMessage(msg);
}
};
public BalanceDataReuqest(Context context, Map<String, String> map) {
mContext = context;
this.map = map;
}
@Override
public void sendRequest(int page, PageAndRefreshRequestCallBack listener) {
if (mCallBack == null) {
mCallBack = listener;
}
this.page = page;
map.put("pageNum", page + "");
map.put("pageSize", "30");
// 此处使用线程模拟数据请求(网络或数据库)过程
getData();
}
private void getData(){ //自定义获取网络数据
//通过Handler方式将页总数以及数据传递到主线程
msg.what = 1;
msg.obj = mdatas;
msg.arg1 = totalPage;
handler.sendMessage(msg);}}
至此,就完成了这个开源包的使用,如题效果如图:
开源包下载路径:github地址https://github.com/tubeber/PageAndPullToRefresh
欢迎交流:lyhwtu@126.com