一、介紹
SmartRefreshLayout
是目前使用比較廣泛的一款下拉刷新和上拉加載庫。實(shí)現(xiàn)起來非常方便,可以一鍵修改全局的刷新樣式。而且該庫已經(jīng)提供了大量的刷新效果,其中包括默認(rèn)的 SwipeRefresh 經(jīng)典風(fēng)格,以及一些高級(jí)的 比如游戲刷新、適用于聊天項(xiàng)目的上拉加載更多等等。

Lottie
是 Airbnb 開源的一款動(dòng)畫庫,該庫的優(yōu)勢(shì)是不需要程序員自己寫 Anim 而是將這些工作交給設(shè)計(jì)師處理,最后拿到到只是一個(gè) Json 文件,通過 Lottie 加載 Json 文件就能展示出動(dòng)畫效果。
Lottie社區(qū)已經(jīng)提供了上千套刷新的 Json 文件,如果對(duì)定制要求不是太高,完全可以拿來用,小到點(diǎn)擊效果,大到刷新,啟動(dòng)頁等等。

二、基本使用
SmartRefreshLayout
- 引入依賴
//默認(rèn)提供三種加載效果
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.4-7'
//提供了多種現(xiàn)成的刷新Header庫,可以直接用,如不需要可以不集成
compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.4-7'
注意:同時(shí)需要檢查是否集成了 appcompat ,如果沒集成,需要添加,版本跟隨當(dāng)前
構(gòu)建的版本號(hào)
compile 'com.android.support:appcompat-v7:26.1.0'
- 布局實(shí)現(xiàn)
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/main_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/main_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
- 刷新和加載
如果需要更換刷新的 Header 只需調(diào)用refreshLayout.setRefreshHeader(new MyRefreshAnimHeader);
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
if(refreshLayout.isLoading()){
//關(guān)閉加載
refreshLayout.finishLoadmore()
}
}
});
refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
@Override
public void onLoadmore(RefreshLayout refreshlayout) {
if(refreshLayout.isRefreshing()){
//關(guān)閉刷新
refreshLayout.finishRefresh();
}
}
});
- 全局加載
需要自定義 Application ,
public class BaseApplication extends Application{
public static BaseApplication mContent;
@Override
public void onCreate() {
super.onCreate();
mContent = this;
}
/*設(shè)置全局的下拉刷新樣式*/
static {
SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
@NonNull
@Override
public RefreshHeader createRefreshHeader(Context context, RefreshLayout refreshLayout) {
return new MyRefreshLottieHeader(mContent);
}
});
}
}
Lottie
- 引入依賴
compile 'com.airbnb.android:lottie:2.2.0'
- 布局 XML
動(dòng)畫 Json 文件需要放在 main 目錄的 assets 中,如果沒有該文件,就手動(dòng)創(chuàng)建一個(gè),再將動(dòng)畫文件拷貝進(jìn)去,然后就可以在布局或者代碼中直接使用。
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/loading_lottie"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
app:lottie_fileName="anim2.json"/>
- 動(dòng)畫開始、結(jié)束
動(dòng)畫的播放和結(jié)束都是只需要一行代碼就可以完成。如果需要在代碼中修改動(dòng)畫文件,只需要調(diào)用mAnimationView.setAnimation(animName);
//開始播放動(dòng)畫
mAnimationView.playAnimation();
//取消播放
mAnimationView.cancelAnimation();
三、結(jié)合Lottie使用
雖然 SmartRefreshLayout 已經(jīng)提供十多種加載的樣式,并且支持對(duì)樣式的顏色等效果的基修改。
但是如果需要實(shí)現(xiàn)其它的加載動(dòng)畫效果,就需要自己來自定義 RefreshHeader。
最簡(jiǎn)單的方法是使用幀動(dòng)畫。但是還需要我們?nèi)ツ玫綆瑘D片,然后再去設(shè)置。
這里我們已經(jīng)引入了 Lottie ,發(fā)現(xiàn)其實(shí)可以利用 Lottie 實(shí)現(xiàn)隨時(shí)一行代碼切換上千種效果豈不是美哉。

- 自定義RefreshHeader
必須要實(shí)現(xiàn)RefreshHeader類,然后針對(duì)提供的方法,來加入我們自定義的元素
//初始化View
private void initView(Context context) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.loading_lottie, this);
mAnimationView = (LottieAnimationView) view.findViewById(R.id.loading_lottie);
}
//開始刷新
@Override
public void onStartAnimator(RefreshLayout layout, int height, int extendHeight) {
mAnimationView.playAnimation();
}
//結(jié)束刷新
@Override
public int onFinish(RefreshLayout layout, boolean success) {
mAnimationView.cancelAnimation();
return 0;
}
- 在自定義 SmartRefresh 中提供 Lottie 動(dòng)畫切換的方法
public void setAnimationViewJson(String animName){
mAnimationView.setAnimation(animName);
}
public void setAnimationViewJson(Animation anim){
mAnimationView.setAnimation(anim);
}
四、一行代碼切換刷新動(dòng)畫
mRefreshLottieHeader.setAnimationViewJson("anim2.json");
Demo地址:https://github.com/wapchief/SmartRefreshLottie
已經(jīng)添加了自定義 Anim 幀動(dòng)畫的切換。Demo下載關(guān)于 SmartRefreshLayout 更詳細(xì)的使用請(qǐng)參考官方文檔
https://github.com/scwang90/SmartRefreshLayoutLottie基本介紹:一款非常好用的動(dòng)畫庫Lottie
Lottie社區(qū) 可以自行下載動(dòng)畫文件