效果圖展示
SmartRefreshLayout是一個(gè)用于上拉加載和下拉刷新的一個(gè)控件盆赤,通常結(jié)合RecyclerView一起使用
SmartRefreshLayout的目標(biāo)是打造一個(gè)強(qiáng)大牺六,穩(wěn)定,成熟的下拉刷新框架畏纲,并集成各種的炫酷春缕、多樣、實(shí)用票灰、美觀的Header和Footer宅荤。 正如名字所說(shuō)膘侮,SmartRefreshLayout是一個(gè)“聰明”或者“智能”的下拉刷新布局,由于它的“智能”逻锐,它不只是支持所有的View,還支持多層嵌套的視圖結(jié)構(gòu)昧诱。 它繼承自ViewGroup 而不是FrameLayout或LinearLayout,提高了性能凶掰。 也吸取了現(xiàn)在流行的各種刷新布局的優(yōu)點(diǎn)蜈亩,包括谷歌官方的 SwipeRefreshLayout, 其他第三方的 Ultra-Pull-To-Refresh畅涂、TwinklingRefreshLayout 午衰。 還集成了各種炫酷的 Header 和 Footer。支持所有的 View(AbsListView臊岸、RecyclerView尊流、WebView....View)
支持自定義并且已經(jīng)集成了很多炫酷的 Header 和 Footer.
1.導(dǎo)入依賴
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-14'
//沒(méi)有使用特殊Header和Footer蜘澜,可以不加下面這行
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-14'
//Recyclerview
implementation 'com.android.support:recyclerview-v7:28.0.0'
2.在activity_main.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"
android:orientation="vertical"
tools:context=".MainActivity">
<!--app:srlAccentColor="#00000000"http://設(shè)置Header主題顏色
app:srlPrimaryColor="#00000000"http://設(shè)置Footer主題顏色
app:srlEnablePreviewInEditMode="true"http://開(kāi)啟和關(guān)閉預(yù)覽功能-->
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.scwang.smartrefresh.header.DeliveryHeader
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
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>
</LinearLayout>
3.MainActivity中使用
//加載更多
refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
page ++ ;
initData();
adapter.notifyDataSetChanged();
refreshLayout.finishLoadMore(true);//加載完成
}
});
//刷新
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
list.clear();
initData();
adapter.notifyDataSetChanged();
refreshLayout.finishRefresh(true);//刷新完成
}
});