分页库自定义数据源从不触发loadBefore

时间:2019-03-05 13:03:08

标签: android androidx android-paging-library

我正在尝试使用Paging Library实现聊天应用程序,但是我的DataSource永远不会触发loadBefore和loadAfter,而是触发loadInitial,并且我可以在RecyclerView中显示初始数据。我也在使用Room。 我已经搜索了一些教程,但是没有一个能帮助我解决这个问题。

有人可以帮助我吗?

谢谢!

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>

1 个答案:

答案 0 :(得分:1)

已解决

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