SmartRefreshLayout——上拉加载下拉刷新

2019-04-09  本文已影响0人  我挺平凡

效果图展示

图片.png

SmartRefreshLayout是一个用于上拉加载和下拉刷新的一个控件,通常结合RecyclerView一起使用

SmartRefreshLayout的目标是打造一个强大,稳定,成熟的下拉刷新框架,并集成各种的炫酷、多样、实用、美观的Header和Footer。 正如名字所说,SmartRefreshLayout是一个“聪明”或者“智能”的下拉刷新布局,由于它的“智能”,它不只是支持所有的View,还支持多层嵌套的视图结构。 它继承自ViewGroup 而不是FrameLayout或LinearLayout,提高了性能。 也吸取了现在流行的各种刷新布局的优点,包括谷歌官方的 SwipeRefreshLayout, 其他第三方的 Ultra-Pull-To-RefreshTwinklingRefreshLayout 。 还集成了各种炫酷的 Header 和 Footer。支持所有的 View(AbsListView、RecyclerView、WebView....View)
支持自定义并且已经集成了很多炫酷的 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"//设置Header主题颜色
    app:srlPrimaryColor="#00000000"//设置Footer主题颜色
    app:srlEnablePreviewInEditMode="true"//开启和关闭预览功能-->

    <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官网查看更多使用细则,这里就不展开讲解了

上一篇 下一篇

猜你喜欢

热点阅读