android下拉刷新上拉加载

https://github.com/miomin/SHSwipeRefreshLayout

SHSwipeRefreshLayout by miomin

支持下拉刷新和上拉加载更多,支持自定义HeaderView和FooterView,支持RecyclerView、ScrollView嵌套滚动,支持所有Layout,支持自定义动画


提示

如使用其它控件时遇到滑动冲突,请参考源码中ShareScrollView、SHListView的实现自行解决,只需让该控件实现NestedScrollingChild接口即可。


简介

  • 使用方法与Google的SwipeRefreshLayout一致,采用内包裹的方式
  • 支持下拉刷新和上拉加载更多
  • 支持通过Resource ID或View自定义HeaderView和FooterView的样式
  • 通过NestedScrolling支持RecyclerView和ScrollView的嵌套滚动不收影响
  • 支持所有Layout、View
  • 支持在回调中设置自定义动画

依赖

  • Step1:在工程build.gradle中添加如下maven仓库
allprojects {
   repositories {
       jcenter()
       // 添加这一行即可
       maven { url "https://raw.githubusercontent.com/miomin/mvn-repo-ione/master" }
   }
}
  • Step2:在Module的build.gradle或者全局添加如下依赖
 compile 'com.miomin:shswiperefreshlayout:1.3.0'

How to use

可参考sample中的示例

In XML

<com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:load_text="加载更多"
        app:progress_bar_color="@color/colorPrimary"
        app:refresh_text="下拉刷新"
        app:guidance_text_color="@color/colorPrimary"
        app:guidance_view_bg_color="@color/transparent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout>

如需使用ScrollView,请使用ShareScrollView

<com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.scu.miomin.shswiperefresh.view.ShareScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        com.scu.miomin.shswiperefresh.view.ShareScrollView>

com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout>

如需使用ListView,请使用SHListView

<com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.scu.miomin.shswiperefresh.view.SHListView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
        com.scu.miomin.shswiperefresh.view.ShareScrollView>

com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout>

其它属性

  • Guidance视图背景颜色 :
<attr name="guidance_view_bg_color" format="color|reference" /> 
  • Guidance视图中文字颜色 :
<attr name="guidance_text_color" format="color|reference" /> 
  • 进度条颜色 :
<attr name="progress_bar_color" format="color|reference" /> 
  • 进度条背景色 :
<attr name="progress_bg_color" format="color|reference" /> 
  • 下拉刷新文字描述 :
<attr name="refresh_text" format="string|reference" /> 
  • 上拉加载文字描述 :
<attr name="load_text" format="string|reference" /> 
  • 下拉刷新是否可用 :
<attr name="pull_refresh_enable" format="boolean" /> 
  • 上拉加载是否可用 :
<attr name="loadmore_enable" format="boolean" /> 

如何自定义HeaderView、FooterView

如果不设置,则使用默认的ProgressBar

可通过如下代码设置 :

  • 设置Resource ID
swipeRefreshLayout.setFooterView(R.layout.refresh_view);
  • 设置View
swipeRefreshLayout.setFooterView(myview);

事件监听

  
        swipeRefreshLayout.setOnRefreshListener(new SHSwipeRefreshLayout.SHSOnRefreshListener() {
               @Override
               public void onRefresh() {
                   swipeRefreshLayout.postDelayed(new Runnable() {
                       @Override
                       public void run() {
                           swipeRefreshLayout.finishRefresh();
                           Toast.makeText(MainActivity.this, "刷新完成", Toast.LENGTH_SHORT).show();
                       }
                   }, 1600);
               }
   
               @Override
               public void onLoading() {
                   swipeRefreshLayout.postDelayed(new Runnable() {
                       @Override
                       public void run() {
                           swipeRefreshLayout.finishLoadmore();
                           Toast.makeText(MainActivity.this, "加载完成", Toast.LENGTH_SHORT).show();
                       }
                   }, 1600);
               }
   
               /**
                * 监听下拉刷新过程中的状态改变
                * @param percent 当前下拉距离的百分比(0-1)
                * @param state 分三种状态{NOT_OVER_TRIGGER_POINT:还未到触发下拉刷新的距离;OVER_TRIGGER_POINT:已经到触发下拉刷新的距离;START:正在下拉刷新}
                */
               @Override
               public void onRefreshPulStateChange(float percent, int state) {
                   switch (state) {
                       case SHSwipeRefreshLayout.NOT_OVER_TRIGGER_POINT:
                           swipeRefreshLayout.setLoaderViewText("下拉刷新");
                           break;
                       case SHSwipeRefreshLayout.OVER_TRIGGER_POINT:
                           swipeRefreshLayout.setLoaderViewText("松开刷新");
                           break;
                       case SHSwipeRefreshLayout.START:
                           swipeRefreshLayout.setLoaderViewText("正在刷新");
                           break;
                   }
               }
   
               @Override
               public void onLoadmorePullStateChange(float percent, int state) {
                   switch (state) {
                       case SHSwipeRefreshLayout.NOT_OVER_TRIGGER_POINT:
                           textView.setText("上拉加载");
                           break;
                       case SHSwipeRefreshLayout.OVER_TRIGGER_POINT:
                           textView.setText("松开加载");
                           break;
                       case SHSwipeRefreshLayout.START:
                           textView.setText("正在加载...");
                           break;
                   }
               }
           });
       }
 
  • 可以在onRefreshPulStateChange和onLoadmorePullStateChange中,根据参数值来做一些自定义动画

其他接口

  • 结束下拉刷新
  
  swipeRefreshLayout.finishRefresh();
 
  • 结束上拉加载
  
  swipeRefreshLayout.finishLoadmore();

你可能感兴趣的:(Android)