使用SmartRefreshLayout下拉刷新框架实现加载更多

使用 SmartRefreshLayout 可以实现recyclerview的下拉刷新和上拉加载更多,但是有时候在使用上拉加载更多时,不同的json格式需要不同的判断方法

1、后台根据每次相加的totalElements的个数作为请求的参数即pageSize+=12   pageNo=1

{"content":[{"cancel":false,"dateCreated":1571369847000,"draft":true,"id":728316,"inApp":true,"inPush":true,"lastUpdated":1571369847000,"messageType":"USER_FOLLOW","messageTypeIndex":503,"module":{"followStatus":2,"imgUrl":"http://chongming-fs.oss-cn-hangzhou.aliyuncs.com/test/APP/545917/user/cd71c19b-f0fb-43eb-9e7f-630fc98dd1cf.jpg","nickname":"*test002","petCategoryName":"哈士奇","petId":1663286,"petName":"啦咯啦咯啦咯","userId":545917},"moduleId":545917,"read":true,"schedule":false,"sendTime":1571369847000,"senderImgUrl":"http://chongming-fs.oss-cn-hangzhou.aliyuncs.com/test/APP/545917/user/cd71c19b-f0fb-43eb-9e7f-630fc98dd1cf.jpg","senderNickName":"*test002","status":"waiting","targetModule":{"messageType":"USER_FOLLOW","status":"CLOSED","targetModuleId":547148},"targetModuleId":547148,"text":"test02关注了你"}],"totalElements":6,"totalPages":1}
    

1.1、举个例子

 

使用SmartRefreshLayout下拉刷新框架实现加载更多_第1张图片

布局样式




    

    

        

        

            

                

                    
                

                

                

            

        

        

    


页面代码

刷新和加载

 refresh.setOnRefreshListener {
            mPageNo = 1
            loadData()
        }
refresh.setOnLoadMoreListener {
            mPageNo++
            mPageSize += 12
            loadData()
        }

回调结果处理

 refresh.finishRefresh()
        refresh.finishLoadMore()
        if (bean.code == "200" && bean.data.content != null) {
            tips.visibility = View.GONE
            recyclerview.visibility = View.VISIBLE
            ll_blank_view.visibility = View.GONE//空白页
            if (mPageNo == 1) {
                adapter!!.setNewData(bean.data.content!!)
                if (bean.data.totalElements < 12) {
                    if (bean.data.totalElements == 0) {
                        tips.visibility = View.GONE
                        recyclerview.visibility = View.GONE
                        ll_blank_view.visibility = View.VISIBLE
                        RecyclerVeiwBlankChangePicUtils.setBlankPic(mContext, "还没留下记录哦", tv_blank, iv_blank)
                    }
                    refresh.setEnableLoadMore(false)
                } else {
                    refresh.setEnableLoadMore(true)
                    tips.visibility = View.GONE
                }
            } else {
                adapter!!.addData(bean.data.content!!)
                if (bean.data.totalElements < mPageSize) {
                    refresh.finishLoadMoreWithNoMoreData()
                    tips.visibility = View.VISIBLE
                } else {
                    refresh.setEnableLoadMore(true)
                }
            }
        }

2、这一种使用的是pageNo++,pageSize是个固定值

private fun loadMore() {

        mPageNo += 1
        XLog.e("mPageNo", mPageNo)
        pageRequest!!.PageNo = mPageNo
        presenter!!.youLove(pageRequest)
    }
refreshLayout.finishRefresh()
        refreshLayout.finishLoadMore()
        if (data == null) {
            love_title.visibility = View.GONE
            rv_love.visibility = View.GONE
        } else {
            love_title.visibility = View.VISIBLE
            rv_love.visibility = View.VISIBLE
            if (mPageNo == 1) {
                loveAdapter!!.setNewData(data)
                if (data.size < 10) {
                    refreshLayout.setNoMoreData(true)
                } else {
                    refreshLayout.setNoMoreData(false)
                }
            } else {
                loveAdapter!!.addData(data)
                if (data.size < 10) {
                    refreshLayout.finishLoadMoreWithNoMoreData()
                } else {
                    refreshLayout.setEnableLoadMore(true)
                }

            }

        }

 

你可能感兴趣的:(Android)