这是一个可自定义,多功能,“智能”的下拉刷新布局。它继承自ViewGroup 提高了性能。 也吸取了现在流行的各种刷新布局的优点,包括谷歌官方的SwipeRefreshLayout。还集成了各种炫酷的 Header 和 Footer。
导入依赖
在build.gradle中添加依赖
//1.1.0 API改动过大,老用户升级需谨慎
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-14'
compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-14'//没有使用特殊Header,可以不加这行
compile 'com.android.support:appcompat-v7:25.3.1'//版本 23以上(必须)
//1.0.5 当1.1.0出现问题可以回退到1.0.5.1
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.5.1'
compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.5.1'//没有使用特殊Header,可以不加这行
compile 'com.android.support:appcompat-v7:25.3.1'//版本 23以上(必须)
compile 'com.android.support:design:25.3.1'//版本随意(非必须,引用可以解决无法预览问题)
Application中全局设置Header 和 Footer
注意:全局中设置的Header和Footer的优先级是最低的,xml中设置的样式是中等,java代码中设置的样式等级最高
- 方法一:全局设置(优先级最低)
public class App extends Application {
//static 代码段可以防止内存泄露
static {
//设置全局的Header构建器
SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
@Override
public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局设置主题颜色
return new ClassicsHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header,默认是 贝塞尔雷达Header
}
});
//设置全局的Footer构建器
SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
@Override
public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
//指定为经典Footer,默认是 BallPulseFooter
return new ClassicsFooter(context).setDrawableSize(20);
}
});
}
}
- 方法二:XML布局文件指定(优先级中等)
<com.scwang.smartrefresh.layout.SmartRefreshLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#444444"
app:srlPrimaryColor="#444444"
app:srlAccentColor="@android:color/white"
app:srlEnablePreviewInEditMode="true">
<!--srlAccentColor srlPrimaryColor 将会改变 Header 和 Footer 的主题颜色-->
<!--srlEnablePreviewInEditMode 可以开启和关闭预览功能-->
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/padding_common"
android:background="@android:color/white"
android:text="@string/description_define_in_xml"/>
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
- 方法三:Java代码设置(优先级最高)
final RefreshLayout refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
//设置 Header 为 贝塞尔雷达 样式
refreshLayout.setRefreshHeader(new BezierRadarHeader(this).setEnableHorizontalDrag(true));
//设置 Footer 为 球脉冲 样式
refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
实现
XML布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:overScrollMode="never" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
代码:设置监听
RefreshLayout mRefreshLayout = findViewById(R.id.refreshLayout);
mRefreshLayout.setOnRefreshLoadMoreListener(this);
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
/**
//加载更多数据业务
**/
//完成加载更多
mRefreshLayout.finishLoadMore();
}
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
/**
//下拉刷新业务
**/
//完成刷新
mRefreshLayout.finishLoadMore();
}
常用得方法
方法 | 说明 |
---|---|
refreshLayout.autoRefresh(); | 自动刷新 |
refreshLayout.autoLoadMore(); | 自动加载更多 |
refreshLayout.finishRefresh(); | 刷新完成 |
refreshLayout.finishRefresh(int delayed); | 刷新完成, 参数delayed 设置延时 |
refreshLayout.finishLoadMore(); | 加载完成 |
refreshLayout.finishLoadMore(int delayed); | 加载完成, 参数delayed 设置延时这两个方法是在加载失败时调用的 |
refreshLayout.finishRefresh(false); | 结束刷新(刷新失败) |
refreshLayout.finishLoadMore(false); | 结束加载(加载失败) |
refreshLayout.finishLoadMoreWithNoMoreData(); | //全部加载完成,没有数据了调用此方法隐藏刷新加载的方法 |
refreshLayout.setEnableLoadMore(false); | 隐藏加载,不使用底部加载 |
refreshLayout.setEnableRefresh(false); | 不使用刷新,隐藏刷新 |
refreshLayout.setNoMoreData(false); | 恢复没有更多数据的原始状态,版本 1.0.5 |
属性讲解
<attr name="srlDrawableSize" format="dimension"/><!--图片尺寸-->
<attr name="srlDrawableArrowSize" format="dimension"/><!--箭头图片尺寸-->
<attr name="srlDrawableProgressSize" format="dimension"/><!--箭头图片尺寸-->
<attr name="srlDrawableMarginRight" format="dimension"/><!--图片和文字的间距-->
<attr name="srlTextSizeTitle" format="dimension"/><!--标题字体-->
<attr name="srlTextSizeTime" format="dimension"/><!--时间字体-->
<attr name="srlFinishDuration" format="integer"/><!--完成时停留时间-->
<attr name="srlPrimaryColor" format="color"/><!--主要颜色-->
<attr name="srlAccentColor" format="color"/><!--强调颜色-->
<attr name="srlDrawableArrow" format="reference"/><!--箭头图片-->
<attr name="srlDrawableProgress" format="reference"/><!--转动图片-->
<attr name="srlEnableHorizontalDrag" format="boolean"/><!--支持水平拖动-->
<attr name="srlClassicsSpinnerStyle" format="enum">
<enum name="Translate" value="0"/><!--平行移动-->
<enum name="Scale" value="1"/><!--拉伸形变-->
<enum name="FixedBehind" value="2"/><!--固定在背后-->
</attr>
<attr name="layout_srlSpinnerStyle" format="enum">
<enum name="Translate" value="0"/><!--平行移动-->
<enum name="Scale" value="1"/><!--拉伸形变-->
<enum name="FixedBehind" value="2"/><!--固定在背后-->
<enum name="FixedFront" value="3"/><!--固定在前面-->
<enum name="MatchLayout" value="4"/><!--填满布局-->
</attr>
<declare-styleable name="SmartRefreshLayout">
<attr name="srlPrimaryColor"/> <!--主题颜色-->
<attr name="srlAccentColor"/> <!--强调颜色-->
<attr name="srlReboundDuration" format="integer"/> <!--释放后回弹动画时长-->
<attr name="srlHeaderHeight" format="dimension"/> <!--Header的标准高度-->
<attr name="srlFooterHeight" format="dimension"/> <!--Footer的标准高度-->
<attr name="srlHeaderInsetStart" format="dimension"/>
<attr name="srlFooterInsetStart" format="dimension"/>
<attr name="srlDragRate" format="float"/> <!--拖动高度设置-->
<attr name="srlHeaderMaxDragRate" format="float"/> <!--Header最大拖动高度/Header标准高度(默认2,要求>=1)-->
<attr name="srlFooterMaxDragRate" format="float"/> <!--Footer最大拖动高度/Footer标准高度(默认2,要求>=1)-->
<attr name="srlHeaderTriggerRate" format="float"/> <!--设置Header触发刷新距离 与 HeaderHieght 的比率(默认1)-->
<attr name="srlFooterTriggerRate" format="float"/> <!--设置Footer触发刷新距离 与 FooterHieght 的比率(默认1)-->
<attr name="srlEnableRefresh" format="boolean"/> <!--是否开启下拉刷新功能(默认true)-->
<attr name="srlEnableLoadMore" format="boolean"/> <!--是否开启加上拉加载功能(默认true)-->
<attr name="srlEnableHeaderTranslationContent" format="boolean"/> <!--拖动Header的时候是否同时拖动内容(默认true)-->
<attr name="srlEnableFooterTranslationContent" format="boolean"/> <!--拖动Footer的时候是否同时拖动内容(默认true)-->
<attr name="srlHeaderTranslationViewId" format="reference"/>
<attr name="srlFooterTranslationViewId" format="reference"/>
<attr name="srlEnablePreviewInEditMode" format="boolean"/> <!--是否在编辑模式时显示预览效果(默认true)-->
<attr name="srlEnableAutoLoadMore" format="boolean"/>
<attr name="srlEnableOverScrollBounce" format="boolean"/>
<attr name="srlEnablePureScrollMode" format="boolean"/>
<attr name="srlEnableNestedScrolling" format="boolean"/>
<attr name="srlEnableScrollContentWhenLoaded" format="boolean"/> <!--是否在加载完成之后滚动内容显示新数据-->
<attr name="srlEnableScrollContentWhenRefreshed" format="boolean"/> <!--是否在刷新完成之后滚动内容显示新数据-->
<attr name="srlEnableLoadMoreWhenContentNotFull" format="boolean"/>
<attr name="srlEnableFooterFollowWhenLoadFinished" format="boolean"/>
<attr name="srlEnableClipHeaderWhenFixedBehind" format="boolean"/>
<attr name="srlEnableClipFooterWhenFixedBehind" format="boolean"/>
<attr name="srlEnableOverScrollDrag" format="boolean"/>
<attr name="srlDisableContentWhenRefresh" format="boolean"/> <!--是否在刷新的时候禁止内容的一切手势操作(默认false)-->
<attr name="srlDisableContentWhenLoading" format="boolean"/> <!--是否在加载的时候禁止内容的一切手势操作(默认false)-->
<attr name="srlFixedHeaderViewId" format="reference"/>
<attr name="srlFixedFooterViewId" format="reference"/>
</declare-styleable>
SmartRefreshLayout炫酷得刷新头
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srlEnableFooterFollowWhenLoadFinished="true"
app:srlEnableScrollContentWhenLoaded="true"
android:layout_below="@+id/toolbar"
>
<!--盒子掉落的样式 -->
<com.scwang.smartrefresh.header.DropBoxHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
></android.support.v7.widget.RecyclerView>
<!--经典的加载样式-->
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srlClassicsSpinnerStyle="Translate" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
要想设置其他效果只要替换com.scwang.smartrefresh.header.DropBoxHeader
这个布局就可以了。DropBoxHeader这个类直接百度找就可以了,还有其他特效:全屏水滴、战争城市游戏头、打砖块游戏等
结束
SmartRefreshLayou得使用方法大概也介绍完了