基于ListView和SwipeRefreshLayout的刷新加载

基于ListView和SwipeRefreshLayout的刷新加载(楼主亲测好使)

先看效果:


aada.gif

介绍使用方法:
用了谷歌的SwipeRefreshLayout作为刷新控件:
xml配置:

1 (2).png

使用方法:
使用这个刷新加载需要注意 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
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@color/transparent"
   android:gravity="center_vertical"
   android:padding="7dp">

   <ProgressBar
       android:id="@+id/listview_load_pb"
       style="?android:attr/progressBarStyle"
       android:layout_width="20dp"
       android:layout_height="20dp"
       android:layout_alignParentLeft="true"
       android:layout_alignParentStart="true"
       android:layout_alignParentTop="true"
       android:layout_marginLeft="30dp"
       android:layout_marginStart="56dp" />

   <TextView
       android:id="@+id/listview_load_tv"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true"
       android:text="@string/refresh_more"
       android:textColor="@color/TextColorGray"
       android:textSize="15sp" />
</RelativeLayout>

轮子的原理,集成和使用都比较简单,
如有bug,请留言;

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 125,135评论 16 537
  • 一提起沈从文,估计很多人就会想起他的爱情名言: 我一辈子走过许多地方的路,行过许多地方的桥,看过许多次数的云,喝过...
    清芷阅读 2,782评论 4 13
  • 最近发生了好多事,情绪一度很是低落。 我把它归结为内心的不够强大。 其实只要一直往前走就是了。想得越多也只能拖累现...
    木柚酱阅读 20评论 0 0
  • 参考书目《回到初衷:一个日本创意鬼才的工作术》 每个人身边都有那么一两个脑洞大过天际的朋友。同一件事,从他们的眼里...
    萌小Q在路上阅读 69评论 9 11
  • 有一个说法,为何读小说,是因为人生很短,几十年的时间不够支持我们去经历各式各样的生活,所以只能够到小说里去。生活没...
    江夏沛阅读 44评论 0 1