SmoothRefreshLayout
项目地址
SmoothRefreshLayout
English | 中文
一个高效的Android刷新库,理论上支持所有的视图,比官方的SwipeRefreshLayout更强大且使用方便.
本开源库的部分代码来自android-Ultra-Pull-To-Refresh.
非常感谢他提供的这么棒的开源项目!
特性:
- 理论上支持所有的视图,且可根据具体需求高效适配.
- 支持5种模式,NONE(做为FrameLayout使用)、REFRESH(头部刷新)、LOAD_MORE(底部刷新)、BOTH(头部刷新和底部刷新)、OVER_SCROLL(越界回弹).
- 支持嵌套滑动,完整实现了NestedScrollingChild,NestedScrollingParent 接口,玩转CoordinatorLayout.
- 直接继承自ViewGroup,拥有卓越的性能,支持类FameLayout的特性(Gravity、Margin).
- 支持自动刷新、自动上拉加载、到底自动加载更多(不推荐,建议使用Adapter实现).
- 支持越界回弹.
- 支持抽屉效果.
- 支持二级刷新事件(TwoLevelSmoothRefreshLayout).
- 支持ListView,RecyclerView加载更多的平滑滚动.
- 支持内容视图的Margin,PS:滚动中没有了Margin效果?SmoothRefreshLayout不存在这种问题.
- 丰富的回调接口和调试信息,可利用现有Api实现丰富的效果.
演示程序
下载 Demo.apk
快照
- 测试QQ浏览器样式
- 测试QQ活动样式
- 测试2级刷新
- 包含FrameLayout
- 包含TextView
- 包含ListView
- 包含GridView
- 包含RecyclerView
- 包含ViewPager
- 包含WebView
- CoordinatorLayout
- 越界回弹模式
- 测试嵌套滑动
使用
Gradle
repositories {
...
maven { url 'https://jitpack.io' }
}
dependencies {
compile 'com.github.dkzwm:SmoothRefreshLayout:1.3.2'
}
在Xml中配置
<?xml version="1.0" encoding="utf-8"?>
<me.dkzwm.smoothrefreshlayout.SmoothRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/smoothRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView_"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</me.dkzwm.smoothrefreshlayout.SmoothRefreshLayout>
Java代码配置
SmoothRefreshLayout refreshLayout = (SmoothRefreshLayout)findViewById(R.id.smoothRefreshLayout);
refreshLayout.setMode(SmoothRefreshLayout.MODE_BOTH);
refreshLayout.setHeaderView(new ClassicHeader(this));
refreshLayout.setOnRefreshListener(new RefreshingListenerAdapter() {
@Override
public void onRefreshBegin(boolean isRefresh) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
refreshLayout.refreshComplete();
}
}, 4000);
}
});
自定义刷新视图
- 接口定义
public interface IRefreshView {
byte TYPE_HEADER = 0;
byte TYPE_FOOTER = 1;
/**
* 返回是头部视图还是尾部视图
*/
int getType();
/**
* 一般情况都是View实现本接口,所以返回this;
*/
View getView();
/**
* 手指离开屏幕
*/
void onFingerUp(SmoothRefreshLayout layout, IIndicator indicator);
/**
* 重置视图
*/
void onReset(SmoothRefreshLayout layout);
/**
* 重新配置视图,准备刷新
*/
void onRefreshPrepare(SmoothRefreshLayout layout);
/**
* 开始刷新
*/
void onRefreshBegin(SmoothRefreshLayout layout, IIndicator indicator);
/**
* 刷新完成
*/
void onRefreshComplete(SmoothRefreshLayout layout);
/**
* 当头部或者尾部视图发生位置变化
*/
void onRefreshPositionChanged(SmoothRefreshLayout layout, byte status, IIndicator indicator);
}
- 添加自定义刷新视图
- 代码添加
setHeaderView(@NonNull IRefreshView header);
setFooterView(@NonNull IRefreshView footer);
- 请直接写入Xml文件,SmoothRefreshLayout会根据添加的View是否是实现了IRefreshView接口进行判断
Xml属性
SmoothRefreshLayout 自身配置
名称 | 类型 | 描述 |
sr_mode | enum | |
sr_content | integer | 指定内容视图的资源ID |
sr_resistance | float | |
sr_resistance_of_pull_up | float | |
sr_resistance_of_pull_down | float | |
sr_ratio_of_refresh_height_to_refresh | float | |
sr_ratio_of_header_height_to_refresh | float | |
sr_ratio_of_footer_height_to_refresh | float | |
sr_offset_ratio_to_keep_refresh_while_Loading | float | |
sr_offset_ratio_to_keep_header_while_Loading | float | |
sr_offset_ratio_to_keep_footer_while_Loading | float | |
sr_can_move_the_max_ratio_of_refresh_height | float | |
sr_can_move_the_max_ratio_of_header_height | float | |
sr_can_move_the_max_ratio_of_footer_height | float | |
sr_duration_to_close_of_refresh | integer | |
sr_duration_to_close_of_header | integer | |
sr_duration_to_close_of_footer | integer | |
sr_duration_of_back_to_refresh_height | integer | |
sr_duration_of_back_to_header_height | integer | |
sr_duration_of_back_to_footer_height | integer | |
sr_enable_pin_content | boolean | |
sr_enable_keep_refresh_view | boolean | |
sr_enable_pull_to_refresh | boolean | |
sr_enable_over_scroll | boolean | |
SmoothRefreshLayout包裹内部其他View支持配置
名称 | 类型 | 描述 |
sr_layout_gravity | flag | 指定其它被包裹视图的对齐属性(非content view、非refresh view) |
Java属性设置方法
名称 | 参数 | 描述 |
setHeaderView | IRefreshView | 配置头部视图 |
setFooterView | IRefreshView | 配置尾部视图 |
setContentView | View | 配置内容视图 |
setEnableNextPtrAtOnce | boolean | 刷新完成即可再次刷新 |
setResistance | float | |
setResistanceOfPullUp | float | |
setResistanceOfPullDown | float | |
setRatioOfRefreshViewHeightToRefresh | float | |
setRatioOfHeaderHeightToRefresh | float | |
setRatioOfFooterHeightToRefresh | float | |
setOffsetRatioToKeepRefreshViewWhileLoading | float | |
setOffsetRatioToKeepHeaderWhileLoading | float | |
setOffsetRatioToKeepFooterWhileLoading | float | |
setCanMoveTheMaxRatioOfRefreshViewHeight | float | |
setCanMoveTheMaxRatioOfHeaderHeight | float | |
setCanMoveTheMaxRatioOfFooterHeight | float | |
setDurationToClose | int | |
setDurationToCloseHeader | int | |
setDurationToCloseFooter | int | |
setDurationOfBackToRefreshViewHeight | integer | |
setDurationOfBackToHeaderHeight | integer | |
setDurationOfBackToFooterHeight | integer | |
setEnablePinContentView | boolean | |
setEnabledPullToRefresh | boolean | |
setEnableOverScroll | boolean | |
setEnabledInterceptEventWhileLoading | boolean | |
setEnableHeaderDrawerStyle | boolean | |
setEnableFooterDrawerStyle | boolean | |
setDisablePerformRefresh | boolean | |
setDisablePerformLoadMore | boolean | |
setDisableRefresh | boolean | |
setDisableLoadMore | boolean | |
setEnableKeepRefreshView | boolean | |
setEnableWhenScrollingToBottomToPerformLoadMore | boolean | |
setEnablePinRefreshViewWhileLoading | boolean | |
回调
名称 | 参数 | 描述 |
setOnRefreshListener | T extends OnRefreshListener | 刷新事件监听回调 |
addOnUIPositionChangedListener | OnUIPositionChangedListener | 添加视图位置变化的监听回调 |
removeOnUIPositionChangedListener | OnUIPositionChangedListener | 移除视图位置变化的监听回调 |
setOnLoadMoreScrollCallback | OnLoadMoreScrollCallback | Footer完成刷新后进行平滑滚动的回调 |
setOnChildScrollUpCallback | OnChildScrollUpCallback | |
setOnChildScrollDownCallback | OnChildScrollDownCallback | |
setOnHookHeaderRefreshCompleteCallback | OnHookUIRefreshCompleteCallBack | 设置Header刷新完成的Hook回调,可实现延迟完成刷新 |
setOnHookFooterRefreshCompleteCallback | OnHookUIRefreshCompleteCallBack | 设置Footer刷新完成的Hook回调,可实现延迟完成刷新 |
其它
名称 | 参数 | 描述 |
debug | boolean | Debug开关 |
refreshComplete | 无参 | 刷新完成,且设置最后一次刷新状态为成功 |
refreshComplete | boolean | 刷新完成,参数:设置最后一次刷新是否刷新成功 |
setLoadingMinTime | long | |
autoRefresh | 无参 | 自动触发Header刷新,立即触发刷新事件并滚动到触发Header刷新位置 |
autoRefresh | boolean | 自动触发Header刷新,参数:是否立即触发刷新事件,会滚动到触发Header刷新位置 |
autoRefresh | boolean,boolean | 自动触发Header刷新,参数1:是否立即触发刷新事件,参数2:是否滚动到触发Header刷新位置 |
autoLoadMore | 无参 | 自动触发Footer刷新,立即触发刷新事件并滚动到触发Footer刷新位置 |
autoLoadMore | boolean | 自动触发Footer刷新,参数:是否立即触发刷新事件,会滚动到触发Footer刷新位置 |
autoLoadMore | boolean,boolean | 自动触发Footer刷新,参数1:是否立即触发刷新事件,参数2:是否滚动到触发Footer刷新位置 |
setLoadMoreScrollTargetView | View | 设置Footer移动时,响应移动事件的内容视图,例如在SmoothRefreshLayout中有一个CoordinatorLayout,CoordinatorLayout中有AppbarLayout、RecyclerView等,加载更多时希望被移动的视图为RecyclerView而不是CoordinatorLayout,那么设置RecyclerView为TargetView即可 |
Thanks
- liaohuqiu android-Ultra-Pull-To-Refresh
- pnikosis material-progress
License
MIT License
Copyright (c) 2017 dkzwm
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.