又一个上拉加载下拉刷新开源库。

我先贴上github地址:https://github.com/scwang90/SmartRefreshLayout

1.在 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'//版本随意(非必须,引用可以解决无法预览问题)

布局:

  <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/smart_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:background="@android:color/white"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

Activity:

 SmartRefreshLayout mSmartRefresh = (SmartRefreshLayout) findViewById(R.id.smart_refresh);

下拉上拉监听

   //BezierRadar样式Header
        mSmartRefresh.setRefreshHeader(new BezierRadarHeader(this).setEnableHorizontalDrag(true));
        //设置 Footer 为 球脉冲 样式
        mSmartRefresh.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
        mSmartRefresh.setEnableRefresh(true);
        //刷新监听
        mSmartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                //refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                Toast.makeText(TestActivity.this, "刷新", Toast.LENGTH_SHORT).show();
            }
        });
        mSmartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //mSmartRefresh.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                Toast.makeText(TestActivity.this, "加载更多", Toast.LENGTH_SHORT).show();
            }
        });
    }
 //停止加载更多
    public void finishLoadMoreData(){
        mSmartRefresh.finishLoadMore(true);
    }
    //停止刷新
    public void finishRefreshData(){
        mSmartRefresh.finishRefresh(true);
    }

猜你喜欢

转载自blog.csdn.net/weixin_42493749/article/details/82423602