【基础笔记】SmartRefreshLayout的简单使用

什么是SmartRefreshLayout

是一个“聪明”或者“智能”的下拉刷新布局,由于它的“智能”,它不只是支持所有的View,还支持多层嵌套的视图结构。 它继承自ViewGroup 而不是FrameLayout或LinearLayout,提高了性能。


如何使用SmartRefreshLayout

一、在build.gradle中添加依赖
// 注意:分包之后不会有默认的Header和Footer需要手动添加!还是原来的三种方法!
implementation 'com.scwang.smart:refresh-layout-kernel:2.0.0-alpha-1' //核心必须依赖
implementation 'com.scwang.smart:refresh-header-classics:2.0.0-alpha-1' //经典刷新头
implementation 'com.scwang.smart:refresh-header-radar:2.0.0-alpha-1' //雷达刷新头
implementation 'com.scwang.smart:refresh-header-falsify:2.0.0-alpha-1' //虚拟刷新头
implementation 'com.scwang.smart:refresh-header-material:2.0.0-alpha-1' //谷歌刷新头
implementation 'com.scwang.smart:refresh-header-two-level:2.0.0-alpha-1' //二级刷新头
implementation 'com.scwang.smart:refresh-footer-ball:2.0.0-alpha-1' //球脉冲加载
implementation 'com.scwang.smart:refresh-footer-classics:2.0.0-alpha-1' //经典加载

根据个人需求,自行添加

如果使用 AndroidX 在 gradle.properties 中添加

android.useAndroidX=true
android.enableJetifier=true
二、xml布局



    
        
        
            
            
                
                
            
        
        
    



刷新控件中放了一个RelativeLayout,里面有一个ListView和空白时显示的提示。

三、Activity

public class ActAllDealsList extends Activity implements OnRefreshListener, OnLoadMoreListener {
    @BindView(R.id.title_my_deals_list)
    TitleBar titleMyDealsList;
    @BindView(R.id.list_view)
    ListView listView;
    @BindView(R.id.layout_blank)
    LinearLayout layoutBlank;
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refreshView;

    private PromptDialog dialog;
    private DealsListAdapter adapter;
    private int pageIndex = Constant.PAGE_START;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_deals_list);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        titleMyDealsList.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        refreshView.setOnRefreshListener(this);//使用该方法时,Activity需要继承OnRefreshListener
        refreshView.setOnLoadMoreListener(this);//使用该方法时,Activity需要继承OnRefreshListener
        //List的相关操作
        adapter = new DealsListAdapter(this);
        listView.setAdapter(adapter);
        listView.setDividerHeight(0);
        onRefresh(refreshView);//下拉刷新控件
    }

    /**
     * 提交查询信息
     */
    private void getData() {
        if (dialog == null){
            dialog = new PromptDialog(this);
        }
        dialog.showInfo("正在加载");
        refreshView.finishRefresh(true);//设置下拉完成刷新
        refreshView.finishLoadMore(true);//设置上拉完成刷新
        AppParams params = new AppParams(IpAddress.getUrl(IpAddress.COMPARISON_PROJECT), true);//自定义params的方法
        params.setConnectTimeout(5000);
        x.http().post(params, new Callback.CommonCallback() {
            @Override
            public void onSuccess(DealsListMode result) {
                if (result != null) {
                    if (result.isSuccess()) {
                        List list = result.getList();
                        adapter.initData(list, pageIndex);
                        if (list != null && list.size() != 0) {//分页操作
                            pageIndex += 1;
                            startDate = list.get(list.size() - 1).getRiqi() + "";
                            layoutBlank.setVisibility(View.GONE);
                            if (list.size() < Constant.PAGE_SIZE) {
                                refreshView.setNoMoreData(true);
                            } else {
                                refreshView.setNoMoreData(false);
                            }
                        } else if (pageIndex == Constant.PAGE_START) {
                            layoutBlank.setVisibility(View.VISIBLE);
                        }
                        dialog.showSuccess("加载成功");
                    } else {
                        dialog.showSuccess(result.getMsg());
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                layoutBlank.setVisibility(View.VISIBLE);
                dialog.showError("获取详细失败,请稍后重试");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    /**
     * 下拉重置分页相关参数
     */
    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        refreshView.setNoMoreData(false);
        pageIndex = Constant.PAGE_START;
        startDate = null;
        getData();
    }
    /**
     * 上拉刷新
     */
    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        getData();
    }
}

Activity中先设置上拉、下拉刷新,使Activity implement OnRefreshListener, OnLoadMoreListener,并重写上拉onRefresh下拉onLoadMore的方法。在方法中获取数据,设置上拉、下拉刷新完成refreshView.finishRefresh(true);refreshView.finishLoadMore(true),后进行数据刷新显示(从服务器获取数据,通过ListView的Adapter进行刷新)。根据实际情况对refreshView.setNoMoreData(true)进行设置。

你可能感兴趣的:(【基础笔记】SmartRefreshLayout的简单使用)