我正在尝试使用 Paging Library 实现聊天应用程序,但我的 DataSource 从不触发 loadBefore 和 loadAfter,而是触发 loadInitial,我可以在 RecyclerView 中显示初始数据。我也在用房间。我搜索了一些教程,但没有一个能帮助我解决这个问题。
有谁能够帮我?
谢谢!
聊天数据源工厂.kt
class ChatDataSourceFactory(private val userId : Long, private val partnerId : Long): DataSource.Factory<Long, MessageAttachments>() {
val source = MutableLiveData<ChatDataSource>()
private lateinit var latestSource: ChatDataSource
override fun create(): DataSource<Long, MessageAttachments> {
latestSource = ChatDataSource(userId, partnerId)
source.postValue(latestSource)
return latestSource
}
}
聊天数据源.kt
class ChatDataSource (private val userId : Long, private val partnerId : Long): ItemKeyedDataSource<Long, MessageAttachments>() {
private val compositeDisposable = CompositeDisposable()
override fun loadInitial(params: LoadInitialParams<Long>, callback: LoadInitialCallback<MessageAttachments>) {
compositeDisposable += MessageRepository.getMessageAttachmentsWith(userId, partnerId, params.requestedInitialKey ?: Long.MAX_VALUE, params.requestedLoadSize.toLong())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
callback.onResult(it, 0, it.size)
},
{
Log.e("ChatDataSource", it.toString())
})
}
override fun loadAfter(params: LoadParams<Long>, callback: LoadCallback<MessageAttachments>) {
Log.d("ChatDataSource", "loadAfter")
}
override fun loadBefore(params: LoadParams<Long>, callback: LoadCallback<MessageAttachments>) {
Log.d("ChatDataSource", "loadBefore")
}
override fun getKey(item: MessageAttachments): Long = item.message?.localMessageId ?: -1
override fun invalidate() {
super.invalidate()
if(!compositeDisposable.isDisposed)
compositeDisposable.dispose()
}
}
聊天视图模型.kt
class ChatViewModel(application: Application, partner : Employee) : BaseAndroidViewModel(application){
private val chatDataSourceFactory = ChatDataSourceFactory(UserConfig.userId, partner.employeeId ?: -1)
val chat = LivePagedListBuilder(chatDataSourceFactory, pagedListConfig())
.setBoundaryCallback(object : PagedList.BoundaryCallback<MessageAttachments>(){
override fun onItemAtEndLoaded(itemAtEnd: MessageAttachments) {
super.onItemAtEndLoaded(itemAtEnd)
}
override fun onZeroItemsLoaded() {
super.onZeroItemsLoaded()
}
override fun onItemAtFrontLoaded(itemAtFront: MessageAttachments) {
super.onItemAtFrontLoaded(itemAtFront)
}
}) .build()
private fun pagedListConfig() = PagedList.Config.Builder()
.setInitialLoadSizeHint(20)
.setEnablePlaceholders(false)
.setPageSize(20 * 2)
.build()
}
聊天片段.xml
<layout
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"
tools:context=".fragments.communication.ChatPagedPartnerFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewChat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</layout>