1.添加依赖
implementation ‘com.scwang.smartrefresh:SmartRefreshLayout:1.0.3’
implementation ‘com.scwang.smartrefresh:SmartRefreshHeader:1.0.3’
2.自定义头部
public class MRefreshHeader extends LinearLayout implements RefreshHeader {private ImageView mImage;
// private AnimationDrawable mAnimPull;
// private AnimationDrawable mAnimRefresh;private Animation mAnimRefresh,mAnimPull;/*** 1&#xff0c;构造方法*/public MRefreshHeader(Context context) {this(context, null, 0);}public MRefreshHeader(Context context, &#64;Nullable AttributeSet attrs) {this(context, attrs, 0);}public MRefreshHeader(Context context, &#64;Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);View view &#61; View.inflate(context, R.layout.m_refresh_header, this);mImage &#61; view.findViewById(R.id.iv_refresh_header);}/*** 2&#xff0c;获取真实视图&#xff08;必须返回&#xff0c;不能为null&#xff09;一般就是返回当前自定义的view*/&#64;NonNull&#64;Overridepublic View getView() {return this;}/*** 3&#xff0c;获取变换方式&#xff08;必须指定一个&#xff1a;平移、拉伸、固定、全屏&#xff09;,Translate指平移&#xff0c;大多数都是平移*/&#64;NonNull&#64;Overridepublic SpinnerStyle getSpinnerStyle() {return SpinnerStyle.Translate;}/*** 4&#xff0c;执行下拉的过程** &#64;param percent* &#64;param offset*/&#64;Overridepublic void onPullingDown(float percent, int offset, int headerHeight, int extendHeight) {if (percent < 1) {mImage.setScaleX(percent);mImage.setScaleY(percent);}}/*** 5&#xff0c;一般可以理解为一下case中的三种状态&#xff0c;在达到相应状态时候开始改变* 注意&#xff1a;这三种状态都是初始化的状态*/&#64;Overridepublic void onStateChanged(&#64;NonNull RefreshLayout refreshLayout, &#64;NonNull RefreshState oldState, &#64;NonNull RefreshState newState) {switch (newState) {//1,下拉刷新的开始状态&#xff1a;下拉可以刷新case PullDownToRefresh:mImage.setImageResource(R.drawable.icon_refresh_line_loading_black);break;//2,下拉到最底部的状态&#xff1a;释放立即刷新case ReleaseToRefresh:
// mImage.setImageResource(R.drawable.anim_pull_end);
// mAnimPull &#61; (AnimationDrawable) mImage.getDrawable();
// mAnimPull.start();mImage.setImageResource(R.drawable.icon_refresh_line_loading_black);break;//3,下拉到最底部后松手的状态&#xff1a;正在刷新case Refreshing:
// mImage.setImageResource(R.drawable.anim_pull_refreshing);
// mAnimRefresh &#61; (AnimationDrawable) mImage.getDrawable();
// mAnimRefresh.start();mImage.setImageResource(R.drawable.icon_refresh_line_loading_black);mAnimRefresh &#61; new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);//ani.setFillAfter(boolean); true停留在旋转后的位置 false复位mAnimRefresh.setFillAfter(!mAnimRefresh.getFillAfter());mAnimRefresh.setDuration(500);mAnimRefresh.setRepeatCount(Animation.INFINITE);mImage.startAnimation(mAnimRefresh);break;default:}}/*** 6&#xff0c;结束下拉刷新的时候需要关闭动画** &#64;param refreshLayout* &#64;param success* &#64;return*/&#64;Overridepublic int onFinish(&#64;NonNull RefreshLayout refreshLayout, boolean success) {
// if (mAnimRefresh !&#61; null && mAnimRefresh.isRunning()) {
// mAnimRefresh.stop();
// }
// if (mAnimPull !&#61; null && mAnimPull.isRunning()) {
// mAnimPull.stop();
// }if (mAnimRefresh !&#61; null && mAnimRefresh.hasStarted()) {mAnimRefresh.cancel();}if (mAnimPull !&#61; null && mAnimPull.hasStarted()) {mAnimPull.cancel();}return 0;}&#64;Overridepublic void onReleasing(float percent, int offset, int headerHeight, int extendHeight) {}&#64;Overridepublic void onStartAnimator(&#64;NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {}&#64;Overridepublic void setPrimaryColors(int... colors) {}&#64;Overridepublic void onInitialized(&#64;NonNull RefreshKernel kernel, int height, int maxDragHeight) {}&#64;Overridepublic void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {}&#64;Overridepublic boolean isSupportHorizontalDrag() {return false;}}
头部xml
<LinearLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"xmlns:tools&#61;"http://schemas.android.com/tools"android:layout_width&#61;"match_parent"android:background&#61;"&#64;color/white"android:padding&#61;"5dp"android:gravity&#61;"center"android:layout_height&#61;"wrap_content"><ImageViewandroid:id&#61;"&#64;&#43;id/iv_refresh_header"android:scaleX&#61;"0"android:scaleY&#61;"0"android:translationY&#61;"0dp"android:layout_width&#61;"24dip"android:layout_height&#61;"24dip" />
LinearLayout>
3自定义尾部
public class MRefreshFooter extends LinearLayout implements RefreshFooter {private ImageView mImage;private Animation mAnim;public MRefreshFooter(Context context) {this(context, null, 0);}public MRefreshFooter(Context context, &#64;Nullable AttributeSet attrs) {this(context, attrs, 0);}public MRefreshFooter(Context context, &#64;Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);View view &#61; View.inflate(context, R.layout.m_refresh_footer, this);mImage &#61; view.findViewById(R.id.iv_refresh_footer);mAnim &#61; AnimationUtils.loadAnimation(getContext(), R.anim.anim_round_rotate);LinearInterpolator linearInterpolator &#61; new LinearInterpolator();mAnim.setInterpolator(linearInterpolator);}&#64;NonNull&#64;Overridepublic View getView() {return this;}&#64;NonNull&#64;Overridepublic SpinnerStyle getSpinnerStyle() {return SpinnerStyle.Translate;}&#64;Overridepublic void setPrimaryColors(int... colors) {}&#64;Overridepublic void onInitialized(&#64;NonNull RefreshKernel kernel, int height, int maxDragHeight) {//控制是否稍微上滑动就刷新kernel.getRefreshLayout().setEnableAutoLoadmore(false);}&#64;Overridepublic void onPullingUp(float percent, int offset, int footerHeight, int extendHeight) {}&#64;Overridepublic void onPullReleasing(float percent, int offset, int footerHeight, int extendHeight) {}&#64;Overridepublic boolean setLoadmoreFinished(boolean finished) {return false;}&#64;Overridepublic void onStartAnimator(&#64;NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {}&#64;Overridepublic int onFinish(&#64;NonNull RefreshLayout refreshLayout, boolean success) {if(mAnim !&#61; null && mAnim.hasStarted() && !mAnim.hasEnded()){mAnim.cancel();mImage.clearAnimation();}return 0;}&#64;Overridepublic void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {}&#64;Overridepublic boolean isSupportHorizontalDrag() {return false;}&#64;Overridepublic void onStateChanged(&#64;NonNull RefreshLayout refreshLayout, &#64;NonNull RefreshState oldState, &#64;NonNull RefreshState newState) {switch (newState) {case None:case PullToUpLoad:if (mAnim !&#61; null) {mImage.startAnimation(mAnim);}break;case Loading:case LoadFinish:break;case ReleaseToLoad:break;default:}}}
尾部xml
<LinearLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"android:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"android:background&#61;"&#64;color/white"android:gravity&#61;"center"android:orientation&#61;"vertical"android:padding&#61;"5dp"><ImageViewandroid:id&#61;"&#64;&#43;id/iv_refresh_footer"android:layout_width&#61;"20dp"android:layout_height&#61;"20dp"android:src&#61;"&#64;drawable/icon_refresh_line_loading_black" /><TextViewandroid:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:layout_marginTop&#61;"10dp"android:text&#61;"底部"android:textSize&#61;"11sp" />
LinearLayout>
4.使用示例
<LinearLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"android:layout_width&#61;"match_parent"android:layout_height&#61;"match_parent"android:background&#61;"&#64;android:color/white"android:orientation&#61;"vertical"><Viewandroid:id&#61;"&#64;&#43;id/fuzhu_line_v"android:layout_width&#61;"match_parent"android:layout_height&#61;"0.1dp" /><com.scwang.smartrefresh.layout.SmartRefreshLayoutandroid:id&#61;"&#64;&#43;id/content_sfl"android:layout_width&#61;"match_parent"android:layout_height&#61;"match_parent"android:background&#61;"&#64;android:color/white"><com.xxxx.xxx.xxx.MRefreshHeaderandroid:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content" /><androidx.recyclerview.widget.RecyclerViewandroid:id&#61;"&#64;&#43;id/content_rv"android:layout_width&#61;"match_parent"android:layout_height&#61;"match_parent"android:background&#61;"&#64;android:color/white" /><com.xxx.xxx.xxx.MRefreshFooterandroid:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content" />com.scwang.smartrefresh.layout.SmartRefreshLayout>LinearLayout>
示例资源图片&#xff1a;