gpt4 book ai didi

android - 分页库自定义数据源永远不会触发 loadBefore

转载 作者:行者123 更新时间:2023-11-29 00:55:36 24 4
gpt4 key购买 nike

我正在尝试使用 Paging Library 实现一个聊天应用程序,但我的 DataSource 从未触发 loadBefore 和 loadAfter,但触发了 loadInitial,我可以在我的 RecyclerView 中显示初始数据。我也在用房间。我搜索了一些教程,但没有一个能帮助我解决这个问题。

谁能帮帮我?

谢谢!

ChatDataSourceFactory.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
}

}

ChatDataSource.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()
}
}

ChatViewModel.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()
}

chat_fragment.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>

最佳答案

已解决

我在适配器上使用 currentList?.get(position) 而不是 getItem(position)

关于android - 分页库自定义数据源永远不会触发 loadBefore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55003484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com