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

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

View File

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

View File

@@ -1,8 +1,11 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" <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:id="@+id/refresh_layout"
android:layout_width="match_parent" 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 <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view" android:id="@+id/recycler_view"

View File

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