SwipeToLoadLayout--小白也能轻松定制自己的刷新效果

2024-04-03 21:08

本文主要是介绍SwipeToLoadLayout--小白也能轻松定制自己的刷新效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

刚开始接触android的时候,就想着如果能定制一款属于自己的刷新效果,肯定会是一件很酷的事情,当然了,github上已经有了很多很炫酷的刷新效果,各种漂亮,但在项目中总要讲究个实用性,有些刷新效果是不错,但在实战中就会显得很不搭,无赖只能舍弃,XListView实用,PullToRefresh也实用,用了这么久,是真不想用,在当时是很酷,可这些项目也早就没人维护,刷新效果有些过时,修改起来也不是那么容易,项目中经常要赶进度,so,也一直将就着用,google 提供的SwipeRefreshLayout效果也不赖,但却很局限,样式局限,子布局还一定得是可以滑动的布局(如ListView,ScrollView),还没提供上拉加载的效果,要用还得自己写,还有一点不知道google怎么搞的,SwipeRefreshLayout居然和RecyclerView冲突(连续下拉,刷新样式会一团糟,还会卡住),自家控件还弄成这样,真是的。说了那么一堆,来看看今天的主角 SwipeToLoadLayout,献上两张截图:

刷新框架那么多,为什么我选择了SwipeToLoadLayout?

  • 首先看效果,框架中帮我们实现了几个主流的刷新效果,Twitter style,JD style,google style,Yalantis style,demo也下载下来看了,真不错,还支持各种自定义,自定义头部和尾部,头部还分classic,above,blow,scale四种类型,还有自动刷新的效果,体验也很流畅。

  • 再看代码,刷新,加载各一个接口实现,头部和尾部也都是用接口实现,遵循设计模式的依赖倒转原则原则(针对抽象而不是针对具体编程),所以才能具备那么高可塑性,我们要做的就是实现接口里面的内容就可以轻松写一个刷新效果,就像使用baseAdapter一样,无论什么数据,什么样式,都可以轻松实现。

  • 接着看功能,支持各种View和ViewGroup(ListView,ScrollView,RecyclerView,GridView,WebView,Linearlayout,RelativeLayout,FrameLayout,ImageView,TextView等)的刷新和加载,还支持自动刷新,手动刷新,自动加载,手动加载,禁止刷新,禁止加载等操作,完全满足需求。

    最后,说的这么好,有没有经过测试呢?当然了,口说无凭,带大家实现一个。

通过SwipeToLoadLayout实现一个刷新加载的效果

1、如何集成
Step 1. Add the JitPack repository in your build.gradle at the end of repositories:
[html] view plain copy
print ? 在CODE上查看代码片 派生到我的代码片
  1. repositories {  
  2.     maven { url “https://jitpack.io” }  
  3. }  
repositories {maven { url "https://jitpack.io" }
}
Step 2. Add the dependency in the form


[html] view plain copy
print ? 在CODE上查看代码片 派生到我的代码片
  1. dependencies {  
  2.     compile ‘com.github.Aspsine:SwipeToLoadLayout:v1.0.0’  
  3. }  
dependencies {compile 'com.github.Aspsine:SwipeToLoadLayout:v1.0.0'
}

2,开始自定义刷新效果

swipeToLoadLayout提供了一套接口,刷新的头部自定义一个View实现SwipeTrigger和SwipeRefreshTrigger就行了,刷新的尾部自定义一个View实现SwipeLoadMoreTrigger和SwipeTrigger,头部实现代码:

[java] view plain copy
print ? 在CODE上查看代码片 派生到我的代码片
  1. public class CustomRefreshHeadView extends TextView implements SwipeRefreshTrigger, SwipeTrigger {  
  2.     public CustomRefreshHeadView(Context context) {  
  3.         super(context);  
  4.     }  
  5.   
  6.     public CustomRefreshHeadView(Context context, AttributeSet attrs) {  
  7.         super(context, attrs);  
  8.     }  
  9.   
  10.     public CustomRefreshHeadView(Context context, AttributeSet attrs, int defStyleAttr) {  
  11.         super(context, attrs, defStyleAttr);  
  12.     }  
  13.   
  14.     @Override  
  15.     public void onRefresh() {  
  16.         setText(”正在拼命加载数据…”);  
  17.     }  
  18.   
  19.     @Override  
  20.     public void onPrepare() {  
  21.   
  22.     }  
  23.   
  24.     @Override  
  25.     public void onSwipe(int i, boolean b) {  
  26.         setText(”释放刷新”);  
  27.   
  28.     }  
  29.   
  30.     @Override  
  31.     public void onRelease() {  
  32.   
  33.   
  34.     }  
  35.   
  36.     @Override  
  37.     public void complete() {  
  38.         setText(”刷新成功”);  
  39.     }  
  40.   
  41.     @Override  
  42.     public void onReset() {  
  43.   
  44.     }  
  45. }  
public class CustomRefreshHeadView extends TextView implements SwipeRefreshTrigger, SwipeTrigger {public CustomRefreshHeadView(Context context) {super(context);}public CustomRefreshHeadView(Context context, AttributeSet attrs) {super(context, attrs);}public CustomRefreshHeadView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overridepublic void onRefresh() {setText("正在拼命加载数据...");}@Overridepublic void onPrepare() {}@Overridepublic void onSwipe(int i, boolean b) {setText("释放刷新");}@Overridepublic void onRelease() {}@Overridepublic void complete() {setText("刷新成功");}@Overridepublic void onReset() {}
}

Xml中使用
注意,swipetoloadlayout中布局包裹的View id是指定的,不能乱改,否则找不到

<item name=”swipe_target” type=”id” />刷新目标
<item name=”swipe_refresh_header” type=”id” />刷新头部
<item name=”swipe_load_more_footer” type=”id” />刷新尾部
[html] view plain copy
print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <com.aspsine.swipetoloadlayout.SwipeToLoadLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     xmlns:tools=“http://schemas.android.com/tools”  
  4.     android:id=“@+id/swipeToLoad”  
  5.     android:layout_width=“match_parent”  
  6.     android:layout_height=“match_parent”  
  7.     tools:context=“com.yyydjk.swipetorefreshdemo.MainActivity”>  
  8.   
  9.     <com.yyydjk.swipetorefreshdemo.CustomRefreshHeadView  
  10.         android:id=“@+id/swipe_refresh_header”  
  11.         android:layout_width=“match_parent”  
  12.         android:layout_height=“wrap_content”  
  13.         android:gravity=“center”  
  14.         android:padding=“20dp” />  
  15.   
  16.     <TextView  
  17.         android:id=“@+id/swipe_target”  
  18.         android:layout_width=“match_parent”  
  19.         android:layout_height=“match_parent”  
  20.         android:background=“@color/colorPrimary”  
  21.         android:gravity=“center”  
  22.         android:text=“Hello World!” />  
  23. </com.aspsine.swipetoloadlayout.SwipeToLoadLayout>  
<?xml version="1.0" encoding="utf-8"?>
<com.aspsine.swipetoloadlayout.SwipeToLoadLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/swipeToLoad"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.yyydjk.swipetorefreshdemo.MainActivity"><com.yyydjk.swipetorefreshdemo.CustomRefreshHeadViewandroid:id="@+id/swipe_refresh_header"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:padding="20dp" /><TextViewandroid:id="@+id/swipe_target"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/colorPrimary"android:gravity="center"android:text="Hello World!" />
</com.aspsine.swipetoloadlayout.SwipeToLoadLayout>

代码中调用
[java] view plain copy
print ? 在CODE上查看代码片 派生到我的代码片
  1. CustomRefreshHeadView refreshHeadView = new CustomRefreshHeadView(this);  
  2. refreshHeadView.setPadding(20,20,20,20);  
  3. refreshHeadView.setGravity(Gravity.CENTER);  
  4. refreshHeadView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,  
  5.                 ViewGroup.LayoutParams.WRAP_CONTENT));  
  6. swipeToLoadLayout.setRefreshHeaderView(refreshHeadView);  
CustomRefreshHeadView refreshHeadView = new CustomRefreshHeadView(this);
refreshHeadView.setPadding(20,20,20,20);
refreshHeadView.setGravity(Gravity.CENTER);
refreshHeadView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
swipeToLoadLayout.setRefreshHeaderView(refreshHeadView);

就这么简单,看下演示效果,做的丑了点,以后有时间弄个精致点的


就这么简单,大伙可以试试,更多用法查看原作者项目demo。

这篇关于SwipeToLoadLayout--小白也能轻松定制自己的刷新效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/873984

相关文章

苹果macOS 26 Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色

《苹果macOS26Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色》在整体系统设计方面,macOS26采用了全新的玻璃质感视觉风格,应用于Dock栏、应用图标以及桌面小部件等多个界面... 科技媒体 MACRumors 昨日(6 月 13 日)发布博文,报道称在 macOS 26 Tahoe 中

SpringCloud使用Nacos 配置中心实现配置自动刷新功能使用

《SpringCloud使用Nacos配置中心实现配置自动刷新功能使用》SpringCloud项目中使用Nacos作为配置中心可以方便开发及运维人员随时查看配置信息,及配置共享,并且Nacos支持配... 目录前言一、Nacos中集中配置方式?二、使用步骤1.使用$Value 注解2.使用@Configur

电脑蓝牙连不上怎么办? 5 招教你轻松修复Mac蓝牙连接问题的技巧

《电脑蓝牙连不上怎么办?5招教你轻松修复Mac蓝牙连接问题的技巧》蓝牙连接问题是一些Mac用户经常遇到的常见问题之一,在本文章中,我们将提供一些有用的提示和技巧,帮助您解决可能出现的蓝牙连接问... 蓝牙作为一种流行的无线技术,已经成为我们连接各种设备的重要工具。在 MAC 上,你可以根据自己的需求,轻松地

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

uniapp小程序中实现无缝衔接滚动效果代码示例

《uniapp小程序中实现无缝衔接滚动效果代码示例》:本文主要介绍uniapp小程序中实现无缝衔接滚动效果的相关资料,该方法可以实现滚动内容中字的不同的颜色更改,并且可以根据需要进行艺术化更改和自... 组件滚动通知只能实现简单的滚动效果,不能实现滚动内容中的字进行不同颜色的更改,下面实现一个无缝衔接的滚动

Go语言使用slices包轻松实现排序功能

《Go语言使用slices包轻松实现排序功能》在Go语言开发中,对数据进行排序是常见的需求,Go1.18版本引入的slices包提供了简洁高效的排序解决方案,支持内置类型和用户自定义类型的排序操作,本... 目录一、内置类型排序:字符串与整数的应用1. 字符串切片排序2. 整数切片排序二、检查切片排序状态:

Java实现图片淡入淡出效果

《Java实现图片淡入淡出效果》在现代图形用户界面和游戏开发中,**图片淡入淡出(FadeIn/Out)**是一种常见且实用的视觉过渡效果,它可以用于启动画面、场景切换、轮播图、提示框弹出等场景,通过... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

Docker安装MySQL镜像的详细步骤(适合新手小白)

《Docker安装MySQL镜像的详细步骤(适合新手小白)》本文详细介绍了如何在Ubuntu环境下使用Docker安装MySQL5.7版本,包括从官网拉取镜像、配置MySQL容器、设置权限及内网部署,... 目录前言安装1.访问docker镜像仓库官网2.找到对应的版本,复制右侧的命令即可3.查看镜像4.启