布局要用SmartRefreshLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@mipmap/bg_main"
tools:context=".ui.activity.MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_grade_list"
android:layout_width="match_parent"
android:layout_height="@dimen/dimens_80dp"
android:background="#6A2F80"
android:paddingLeft="@dimen/dimens_30dp"/>
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_no_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:src="@mipmap/icon_nothing"
android:visibility="gone" />
<include
android:id="@+id/status_view"
layout="@layout/status_view_white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"/>
</RelativeLayout>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
刷新加载方法
private fun initRefreshLayout(){
//设置刷新样式
refresh_layout.setRefreshHeader(ClassicsHeader(this))
refresh_layout.setRefreshFooter(ClassicsFooter(this))
//刷新
refresh_layout.setOnRefreshListener {
viewModel.pageIndex = 1
viewModel.GetMicroLesBooks(viewModel.selectGrade)
}
//加载更多
refresh_layout.setOnLoadMoreListener {
viewModel.pageIndex++
viewModel.GetMicroLesBooks(viewModel.selectGrade)
}
refresh_layout.setEnableLoadMore(false)
//刷新状态改变
viewModel.refreshStat.observe(this, Observer {
when (it) {
1 -> {//刷新完成
refresh_layout.finishRefresh()
}
2 -> {//加载完成
refresh_layout.finishLoadMore()
}
3 -> {//没有更多了
ToastUtils.showShort("没有更多了!")
refresh_layout.finishLoadMore()
}
4 -> {//获取数据失败
refresh_layout.finishRefresh()
refresh_layout.finishLoadMore()
if (tempData.size == 0) {
status_view?.visibility = View.VISIBLE
data_available_view.visibility = View.VISIBLE
} else {
status_view?.visibility = View.GONE
data_available_view.visibility = View.GONE
}
}
}
})
}
ViewModel中的操作
var pageIndex = 1
var pageSize = 10000
var selectGrade = "1"
/**
* 刷新状态
* 1 刷新完毕 2 加载完毕 3 没有更多数据了 4 加载出错了
*/
var refreshStat = MutableLiveData<Int>()
var netAvailableLiveData = MutableLiveData<Boolean>()
/**
* 根据年级获取栏目列表和栏目下的教材列表
*/
fun GetMicroLesBooks(gradeId: String) {
if (!NetworkUtils.isConnected()) {
ToastUtils.showShort("请检查您的网络!")
netAvailableLiveData.value = false
return
}
netAvailableLiveData.value = true
var paramsMap = mutableMapOf<String, String>()
paramsMap["gradeId"] = gradeId//默认0,表示所有年级
paramsMap["subjectId"] = "0"//默认0,表示所有科目
paramsMap["pageIndex"] = pageIndex.toString()
paramsMap["pageSize"] = pageSize.toString()
paramsMap["isFilterMicroVideo"] = 1.toString()
// /* isFilterMicroVideo 是否过滤微课栏目下视频:0否,1是(只显示19专题课,1022同步微课栏目下视频),默认是0*/
// if (BuildConfig.APPLICATION_ID === "com.djt.module_lesson_video.xxj") {
//
// } else {
// paramsMap["isFilterMicroVideo"] = 0.toString()
// }
var disposable: Disposable? = null
disposable = LessonVideoApplication.dataManager.GetMicroLesBooks(paramsMap)
.subscribeOn(Schedulers.io()).map {
it.Data?.PageData?.sortBy { it.Sort }
it.Data?.PageData?.forEach {
it.BookList = it.BookList.sortedBy { it.BookSort }
}
it
}
.observeOn(AndroidSchedulers.mainThread()).subscribe({
if (it.Code == AppConstants.Server.SUCCESS_CODE) {
bookList.value = it.Data?.PageData as MutableList<MicroLesBooks>
if (pageIndex == 1) {
refreshStat.value = 1
} else {
if (it.Data!!.TotalPage >= it.Data!!.CurPage) {
refreshStat.value = 2
} else {
refreshStat.value = 3
}
}
} else {
refreshStat.value = 4
ToastUtils.showShort("获取数据失败!")
}
disposable?.dispose()
}, {
disposable?.dispose()
it.printStackTrace()
})
}
网友评论