效果圖展示:
SmartRefreshLayout是一個用于上拉加載和下拉刷新的一個控件,通常結合RecyclerView一起使用
SmartRefreshLayout的目標是打造一個強大键痛,穩(wěn)定消恍,成熟的下拉刷新框架垢袱,并集成各種的炫酷解滓、多樣穗酥、實用瞬浓、美觀的Header和Footer。 正如名字所說机打,SmartRefreshLayout是一個“聰明”或者“智能”的下拉刷新布局矫户,由于它的“智能”,它不只是支持所有的View残邀,還支持多層嵌套的視圖結構皆辽。 它繼承自ViewGroup 而不是FrameLayout或LinearLayout,提高了性能芥挣。 也吸取了現(xiàn)在流行的各種刷新布局的優(yōu)點驱闷,包括谷歌官方的 SwipeRefreshLayout, 其他第三方的 Ultra-Pull-To-Refresh九秀、TwinklingRefreshLayout 。 還集成了各種炫酷的 Header 和 Footer粘我。支持所有的 View(AbsListView鼓蜒、RecyclerView、WebView....View)
支持自定義并且已經(jīng)集成了很多炫酷的 Header 和 Footer.
1.導入依賴
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-14'
//沒有使用特殊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://設置Header主題顏色
app:srlPrimaryColor="#00000000"http://設置Footer主題顏色
app:srlEnablePreviewInEditMode="true"http://開啟和關閉預覽功能-->
<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);//刷新完成
}
});
SmartRefreshLayout關于屬性這一塊也有很多可以設置的都弹,大家可以去SmartRefreshLayout官網(wǎng)查看更多使用細則,這里就不展開講解了