房间相册 照片列表分页问题

This commit is contained in:
wzq
2023-10-18 16:30:01 +08:00
parent 448e6800ab
commit 68c9c6a8cb
4 changed files with 50 additions and 20 deletions

View File

@@ -98,7 +98,7 @@ class RoomAlbumFragment : BaseViewBindingFragment<FragmentRoomAlbumBinding>() {
}.apply {
setEnableLoadMore(true)
setOnLoadMoreListener({
viewModel.loadPhotos(type, page++)
viewModel.loadPhotos(type, ++page)
}, binding.recyclerView)
setOnItemChildClickListener { _, view, position ->
@@ -141,12 +141,7 @@ class RoomAlbumFragment : BaseViewBindingFragment<FragmentRoomAlbumBinding>() {
}
viewModel.myPhotosLiveData.observe(this@RoomAlbumFragment) {
it?.let { list ->
adapter.loadMoreComplete()
if (list.size - adapter.data.size < Constants.PAGE_SIZE) {
adapter.loadMoreEnd()
}
}
adapter.loadMoreComplete()
adapter.setNewDiffData(object : BaseQuickDiffCallback<RoomAlbumPhotoInfo>(it) {
override fun areItemsTheSame(
@@ -169,6 +164,22 @@ class RoomAlbumFragment : BaseViewBindingFragment<FragmentRoomAlbumBinding>() {
viewModel.deleteLiveData.observe(this) {
viewModel.loadPhotos(type)
}
viewModel.loadStateLiveData.observe(this) {
if (it == true) {
adapter.loadMoreComplete()
} else {
if (page > 1) {
page--
}
adapter.loadMoreFail()
}
binding.refreshLayout.finishRefresh()
}
viewModel.noMoreLiveData.observe(this) {
if (it == true) {
adapter.loadMoreEnd()
}
}
}
companion object {

View File

@@ -5,7 +5,9 @@ import androidx.lifecycle.MutableLiveData
import com.hjq.toast.ToastUtils
import com.yizhuan.erban.avroom.bean.RoomAlbumPhotoInfo
import com.yizhuan.erban.base.BaseViewModel
import com.yizhuan.xchat_android_core.Constants
import com.yizhuan.xchat_android_core.manager.AvRoomDataManager
import com.yizhuan.xchat_android_core.utils.toast
class RoomAlbumFragmentViewModel : BaseViewModel() {
@@ -15,19 +17,33 @@ class RoomAlbumFragmentViewModel : BaseViewModel() {
private val _deleteLiveData = MutableLiveData<Boolean?>()
val deleteLiveData: LiveData<Boolean?> = _deleteLiveData
fun loadPhotos(type: Int, page: Int = 1) {
safeLaunch {
val myPhotos = RoomAlbumModel.loadPhotos(AvRoomDataManager.get().roomUid, type, page)
private val _loadStateLiveData = MutableLiveData<Boolean?>()
val loadStateLiveData: LiveData<Boolean?> = _loadStateLiveData
val value = _myPhotosLiveData.value
if (page == 1) {
_myPhotosLiveData.value = myPhotos!!.toMutableList()
return@safeLaunch
private val _noMoreLiveData = MutableLiveData<Boolean?>()
val noMoreLiveData: LiveData<Boolean?> = _noMoreLiveData
fun loadPhotos(type: Int, page: Int = 1) {
safeLaunch(block = {
val data = RoomAlbumModel.loadPhotos(AvRoomDataManager.get().roomUid, type, page)
data?.let {
val value = _myPhotosLiveData.value
if (page == 1) {
_myPhotosLiveData.value = it.toMutableList()
} else {
value?.addAll(it)
_myPhotosLiveData.value = value
}
_loadStateLiveData.value = true
_noMoreLiveData.value = it.size < Constants.PAGE_SIZE
}
value?.addAll(myPhotos!!)
_myPhotosLiveData.value = value
}
}, onError = {
it.message.toast()
_loadStateLiveData.value = false
})
}
fun sendPhoto(photoId: Int) {

View File

@@ -1,8 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
app:srlEnableAutoLoadmore="false"
app:srlEnableLoadmore="false">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"

View File

@@ -31,8 +31,8 @@ COMPILE_SDK_VERSION=33
MIN_SDK_VERSION=21
TARGET_SDK_VERSION=33
version_name=2.0.3
version_code=2003
version_name=2.1.0
version_code=2007
#systemProp.https.proxyHost=127.0.0.1
#systemProp.https.proxyPort=7890