热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SmartRefreshLayout自定义头部刷新和底部加载

1.添加依赖implementation‘com.scwang.smartrefresh:SmartRefreshLayout:1.0.3’implementation‘com.s

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;
icon_refresh_line_loading_black


推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • Android LED 数字字体的应用与实现
    本文介绍了一种适用于 Android 应用的 LED 数字字体(digital font),并详细描述了其在 UI 设计中的应用场景及其实现方法。这种字体常用于视频、广告倒计时等场景,能够增强视觉效果。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文介绍了如何利用JavaScript或jQuery来判断网页中的文本框是否处于焦点状态,以及如何检测鼠标是否悬停在指定的HTML元素上。 ... [详细]
  • 本文详细介绍了如何使用PHP检测AJAX请求,通过分析预定义服务器变量来判断请求是否来自XMLHttpRequest。此方法简单实用,适用于各种Web开发场景。 ... [详细]
  • This guide provides a comprehensive step-by-step approach to successfully installing the MongoDB PHP driver on XAMPP for macOS, ensuring a smooth and efficient setup process. ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
author-avatar
零落曦_622
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有