使用 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、举个例子
布局样式
<?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:background="#fff7f7f7"
android:orientation="vertical"
tools:context=".ui.activity.message.MineFansActivity">
<include
android:id="@+id/top_bar"
layout="@layout/custom_toolbar" />
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srlEnableAutoLoadMore="true"
app:srlEnableLoadMore="true"
app:srlEnableRefresh="true">
<cn.ipets.chongmingandroid.ui.widget.recyclerview.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="UnusedAttribute">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<com.chongminglib.view.VpRecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:targetApi="lollipop" />
</LinearLayout>
<TextView
android:id="@+id/tips"
android:layout_width="match_parent"
android:layout_height="@dimen/dp60"
android:layout_gravity="center_horizontal"
android:background="@color/transparent"
android:gravity="center"
android:text="没有更多内容了"
android:textColor="@color/colorFrame"
android:textSize="@dimen/sp14"
android:visibility="gone" />
<include
android:id="@+id/ll_blank_view"
layout="@layout/layout_blank"
android:visibility="gone" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<cn.ipets.chongmingandroid.ui.widget.recyclerview.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
页面代码
刷新和加载
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)
}
}
}