Android 智能刷新框架SmartRefreshLayout
SmartRefreshLayout是一个“聪明”或者“智能”的下拉刷新布局,由于它的“智能”,它不只是支持所有的View,还支持多层嵌套的视图结构。它继承自ViewGroup 而不是FrameLayout或LinearLayout,提高了性能。 也吸取了现在流行的各种刷新布局的优点,包括谷歌官方的 SwipeRefreshLayout,其他第三方的 Ultra-Pull-To-Refresh、TwinklingRefreshLayout 。还集成了各种炫酷的 Header 和 Footer。 SmartRefreshLayout的目标是打造一个强大,稳定,成熟的下拉刷新框架,并集成各种的炫酷、多样、实用、美观的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'//版本随意(非必须,引用可以解决无法预览问题)
使用指定的Header和Footer
方法一:全局设置(优先级最低,会被下面两种方法取代)
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));
各种效果和显示操作方式
默认使用
布局:根布局为LinearLayout,SmartRefreshLayout直接包裹一个RecyclerView
<?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>
代码:设置刷新监听,下拉刷新功能默认是打开的:mRefreshLayout.setEnableRefresh(true);
RefreshLayout mRefreshLayout = findViewById(R.id.refreshLayout);
mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
List<String> data = initDatas();
Message message = new Message();
message.what = 1 ;
message.obj = data ;
mHandler.sendMessageDelayed(message,2000);
}
});
private Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what){
case 1: //刷新加载
List<String> mList = (List<String>) msg.obj;
mRefreshLayout.finishRefresh(true);
adapter.setDatas(mList);
break;
case 2: //加载更多
List<String> mLoadMoreDatas = (List<String>) msg.obj;
mRefreshLayout.finishLoadMore(true);
adapter.addMoreValue(mLoadMoreDatas);
break;
}
return false;
}
});
经典风格
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
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="#fff"
android:overScrollMode="never" />
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srlClassicsSpinnerStyle="Translate"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
在其中可以根据需要进行各种属性设置; 注:在设置属性时,代码中针对文字大小使用的是sp,故有特殊需求的需要改变设置: 如不随系统字体大小改变的实现方式,或自己设置dp:((TextView)header.findViewById(ClassicsHeader.ID_TEXT_TITLE)).setTextSize(TypedValue.COMPLEX_UNIT_DIP,16);
3 FalsifyHeader 虚假的Header,用于 真正的 Header 在RefreshLayout外部时,使用本虚假的FalsifyHeader填充在RefreshLayout内部,具体使用方法参考纸飞机; 单独使用FalsifyHead的效果为在下拉时出现空白下拉布局,这个也是可以监听下拉滴: FalsifyFooter 上拉加载和Falsifyheader是一样的空白背景效果,是可以拉动监听的
4、自定义Header可以参考Demo中的使用-Custom 5、淘宝二楼header TwoLevelHeader,参考demo中实战-淘宝二楼 下面的为特殊样式header,需要引用:com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-14 6、飞行气球:DeliveryHeader 7、掉落盒子:DropBoxHeader 8、全屏水滴:WaveSwipeHeader 9、官方下拉Meterial:MaterialHeader 10、颜色闪烁渐变头:StoreHouseHeader 11、战争城市游戏头:FunGameBattleCityHeader 12、打砖块游戏:FunGameHitBlockHeader 13、弹出圆圈加载:BezierCircleHeader 14、冲上云霄:TaurusHeader 15、金色校园:PhoenixHeader