基于ListView和SwipeRefreshLayout的刷新加载(楼主亲测好使)
先看效果:
介绍使用方法:
用了谷歌的SwipeRefreshLayout作为刷新控件:
xml配置:
使用方法:
使用这个刷新加载需要注意 3点;
1、RefreshAndload new的时候,需要放在listview.setAdapter后面
2、当网络请求数据,返回后,需要停止加载栏的状态【//停止刷新加载 refreshAndload.stopRefreshAndLoad();】
3、当你的fragment和activity退出时,请尽量 调用RefreshAndLoad这个类的【onDestroy();方法】
![W]8ZTBZL9@540HLY05D7F~W.png](http://upload-images.jianshu.io/upload_images/3066171-62e44cfa4ec3c450.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
下面就是 RefreshAndLoad 类的代码
/**
* 刷新加载
* Created by xz on 2016/10/24 0024.
* 此类通过SwipeRefreshLayout,和listview的head,实现了刷新和加载;
* 此类实现了listview的拉动监听,如果你需要用到listview的拉动监听,请使用本类中的拉动监听;
*/
public class RefreshAndLoad implements SwipeRefreshLayout.OnRefreshListener, View.OnClickListener {
private Context context;
private ListView listView;
private SwipeRefreshLayout swipeRefreshLayout;
private ProgressBar loadPB;//加载的进度条
private TextView loadTV;//加载的文字
private OnLoadListener onLoadListener;//加载
private OnRefreshListener onRefreshListener;//刷新
private OnScrollListener onScrollListener;//拉动加载
private View loadView;
public final int LOADIS = 0;//正在加载
public final int LOADClick = 1;//点击加载更多...
public final int LOADNO = 2;//暂无更多数据可加载
public final int LOADGONE = 3;//隐藏加载栏
public final int LOADVISIBLE = 4;//显示加载栏
public final int LOADERROR = 5;//加载错误
public final int LOADSUCCESS = 6;//加载完成
private boolean isLoad = false;//是否在加载 false-否;true-加载中
private boolean isRefresh = false;//是否在刷新 false-否;true-刷新中
/**
* 只有加载的
*
* @param context
* @param listView
*/
public RefreshAndLoad(Context context, ListView listView) {
init(context, listView, null);
}
/**
* 只有刷新的
*
* @param context
* @param swipeRefreshLayout
*/
public RefreshAndLoad(Context context, SwipeRefreshLayout swipeRefreshLayout) {
init(context, null, swipeRefreshLayout);
}
/**
* 带刷新加载的
*
* @param context
* @param listView
* @param swipeRefreshLayout
*/
public RefreshAndLoad(Context context, ListView listView, SwipeRefreshLayout swipeRefreshLayout) {
init(context, listView, swipeRefreshLayout);
}
/**
* 初始化view
*/
private void init(Context context, ListView listView, SwipeRefreshLayout swipeRefreshLayout) {
this.context = context;
this.listView = listView;
this.swipeRefreshLayout = swipeRefreshLayout;
if (listView != null) {
//加载
loadView = LayoutInflater.from(context).inflate(
R.layout.listview_load, null);
loadView.setOnClickListener(this);
loadPB = (ProgressBar) loadView.findViewById(R.id.listview_load_pb);
loadTV = (TextView) loadView.findViewById(R.id.listview_load_tv);
loadTV.setOnClickListener(this);
listView.addFooterView(loadView, null, false);
ListViewScrollListener();
//默认是隐藏加载栏的
loadStatus(LOADGONE);
}
if (swipeRefreshLayout != null) {
//刷新
swipeRefreshLayout.setColorSchemeResources(R.color.danorange);
swipeRefreshLayout.setOnRefreshListener(this);
}
}
/**
* 监听listview的滑动
*/
public void ListViewScrollListener() {
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (onScrollListener != null) {
onScrollListener.onScrollStateChanged(view, scrollState);
}
//停止滑动
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
//是否显示加载
if (isShowLoad()) {
//是否是加载状态
if (isLoad != true) {
loadStatus(LOADVISIBLE);
onLoad();
} else {
loadStatus(LOADClick);
}
} else {
loadStatus(LOADGONE);
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (onScrollListener != null) {
onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
}
}
});
}
/**
* 判断是否显示加载栏
*
* @return
*/
public boolean isShowLoad() {
//判断listview的条目
if (this.listView.getCount() == 0) {
return false;
}
//判断listview是否滑到最后一个
if (listView.getLastVisiblePosition() == (listView.getCount() - 1)) {
//判断总条目是否大于可见条目,大于才说明,item是超过屏幕的
if (listView.getCount() > listView.getChildCount()) {
return true;
}
}
return false;
}
/**
* 刷新的
*/
@Override
public void onRefresh() {
if (onRefreshListener != null) {
onRefreshListener.onRefresh();
}
}
/**
* 加载的
*/
public void onLoad() {
isLoad = true;
if (onLoadListener != null) {
loadStatus(LOADIS);
onLoadListener.onLoad();
}
}
/**
* 进入app就刷新
*/
public void immediatelyRefresh() {
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
if (swipeRefreshLayout != null) {
swipeRefreshLayout.setRefreshing(true);
onRefresh();
}
}
});
}
/**
* 停止刷新,或者加载
*/
public void stopRefreshAndLoad() {
if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) {
swipeRefreshLayout.setRefreshing(false);
}
if (isLoad()) {
isLoad = false;
loadStatus(LOADClick);
}
}
/**
* 停止刷新,或者加载,
*
* @param status:加载显示的状态
*/
public void stopRefreshAndLoad(int status) {
if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) {
swipeRefreshLayout.setRefreshing(false);
}
if (isLoad()) {
isLoad = false;
loadStatus(status);
}
}
/**
* 停止刷新
*/
public void stopRefresh() {
if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) {
swipeRefreshLayout.setRefreshing(false);
}
}
/**
* 停止加载
*/
public void stopLoad() {
loadStatus(LOADGONE);
}
/**
* int LOADIS = 0;//正在加载
* int LOADClick = 1;//点击加载更多...
* int LOADNO = 2;//暂无更多数据可加载
* int LOADGONE = 3;//隐藏加载栏
* int LOADVISIBLE = 4;//显示加载栏
* int LOADERROR=5;//加载错误
* int LOADSUCCESS = 6;//加载完成
*
* @param status:
*/
public void loadStatus(int status) {
switch (status) {
case LOADIS:
loadView.setVisibility(View.VISIBLE);
loadTV.setText("正在加载...");
loadPB.setVisibility(View.VISIBLE);
break;
case LOADClick:
loadTV.setText("点击加载更多...");
loadPB.setVisibility(View.GONE);
break;
case LOADNO:
loadTV.setText("暂无更多数据可加载");
loadPB.setVisibility(View.GONE);
break;
case LOADGONE:
// loadTV.setText("点击加载更多...");
// loadPB.setVisibility(View.GONE);
loadPB.setVisibility(View.GONE);
loadView.setVisibility(View.GONE);
break;
case LOADVISIBLE:
loadView.setVisibility(View.VISIBLE);
loadStatus(LOADClick);
loadStatus(LOADIS);
break;
case LOADERROR:
loadTV.setText("网络错误,点击重试");
loadPB.setVisibility(View.GONE);
break;
case LOADSUCCESS:
loadTV.setText("加载完成");
loadPB.setVisibility(View.GONE);
break;
}
}
/**
* 刷新此控件
*/
public void onRefreshThis() {
stopRefreshAndLoad();
//是否显示加载
if (isShowLoad()) {
loadStatus(LOADClick);
} else {
loadStatus(LOADGONE);
}
}
/**
* 退出activity的时候,需要移除listview的footerview
*/
public void onDestroy() {
if (listView != null && loadView != null)
listView.removeFooterView(loadView);
}
/**
* 加载栏点击事件
*
* @param v
*/
@Override
public void onClick(View v) {
if (!isLoad()) {
onLoad();
}
}
public boolean isRefresh() {
if (swipeRefreshLayout != null) {
return swipeRefreshLayout.isRefreshing();
} else {
return false;
}
}
// public void setRefresh(boolean refresh) {
// isRefresh = refresh;
// }
public boolean isLoad() {
return isLoad;
}
public void setLoad(boolean load) {
isLoad = load;
}
public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
this.onRefreshListener = onRefreshListener;
}
public void setOnLoadListener(OnLoadListener onLoadListener) {
this.onLoadListener = onLoadListener;
}
public void setOnScrollListener(OnScrollListener onScrollListener) {
this.onScrollListener = onScrollListener;
}
/**
* listview的加载监听
*/
public interface OnLoadListener {
void onLoad();
}
/**
* listview的刷新监听
*/
public interface OnRefreshListener {
void onRefresh();
}
/**
* listview的拉动监听
*/
public interface OnScrollListener {
void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount);
void onScrollStateChanged(AbsListView view, int scrollState);
}
}
```
加载栏【 listview_load.xml 】的代码
```java
轮子的原理,集成和使用都比较简单,
如有bug,请留言;