天天看點

SmartRefreshLayout OkHttpUtils加載重新整理

重新整理的方法

public void getRefreshProductList(){

        JSONObject info = new JSONObject();
        int uid = SoftApplication.softApplication.getUserInfo().uid;

        info.put("pageSize",page+"");//第一:page不變
        info.put("firstResult",0+"");//第二:重新整理的時候直接寫0


        OkHttpUtils.get()
                .url(Constants.server_address+ "index/singleProductList139.do")
                .addParams("auth",SoftApplication.softApplication.getAuthJsonObject(info.toString()))
                .addParams("info",info.toString())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        
                        //mSrl.finishRefresh(0);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        
                        firstResult=1;//第三:重新整理完以後讓firstResult=1
                        mSrl.finishRefresh(0);

                        SingleProduceBean singleProduceBean = JSONObject.parseObject(response, SingleProduceBean.class);
                        List<SingleProduceBean.ResultdataBean.ListBean> list =
                                singleProduceBean.getResultdata().getList();

                        //第四:
                        if(list.size()==0){
                            hasMorePages=false;
                        }else{
                            hasMorePages=true;
                        }

                        mAdapter.setNewData(list);
                        mAdapter.loadMoreComplete();

                    }
                });
    }
           

加載的方法:

public void getProductList(){
        JSONObject info = new JSONObject();
        info.put("pageSize",page+"");//
        info.put("firstResult",firstResult+"");//第一:firstResult

        OkHttpUtils.get()
                .url(Constants.server_address+ "index/singleProductList139.do")
                .addParams("auth",SoftApplication.softApplication.getAuthJsonObject(info.toString()))
                .addParams("info",info.toString())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("SingleProductActivity", " onError: "+e.toString() );
                        //mSrl.finishRefresh(0);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                       
                        //第二:
                        firstResult++;
                        mSrl.finishRefresh(0);

                        SingleProduceBean singleProduceBean = JSONObject.parseObject(response, SingleProduceBean.class);
                        List<SingleProduceBean.ResultdataBean.ListBean> list =
                                singleProduceBean.getResultdata().getList();

                         //第三:
                        if(list.size()==0){
                            hasMorePages=false;
                        }else{
                            hasMorePages=true;
                        }
                        mAdapter.addData(list);
                        mAdapter.loadMoreComplete();
                    }
                });
    }
           

加載的時候:

@Override
    public void onLoadMoreRequested() {

        if(hasMorePages){
            getProductList();
        }else{
            mAdapter.loadMoreEnd(false);
        }

    }