4

I'm implemented paging feature based on PagedListAdapter and I've been checked that works fine. I try implementation to feature as If ListAdapter items are empty then showing EmptyHolder. I have tried that based on commitCallback after called submitList as follows code.

Question

Why has PagedListAdapter item count shows as 0 even though the response contains items? How to solve this problem?

FooDataSource

private suspend fun loadItems(beforeId: String) = withContext(Dispatchers.IO) {
    getFooUseCase(beforeId)
}

override fun loadInitial(
    params: LoadInitialParams<String>,
    callback: LoadInitialCallback<String, Foo>
) {
    launch {
        loadItems(null)
            .run {
                Timber.d("loadInitial : ${list.size}") // onResponse : 20
                callback.onResult(list, null, beforeId)
            }
    }
}

FooListFragment

observe(fooList) { items ->
    // items.size or loadedCount always 0
    fooAdapter.submitList(items) {
        //fooAdapter.itemCount, fooAdapter.currentList.size or loadedSize
        //always 0...
        if(fooAdapter.itemCount == 0) {
            //show EmptyHolder
        }
    }
}
Ethan Choi
  • 2,339
  • 18
  • 28

1 Answers1

0

PagedList.BoundaryCallback have onZeroItemsLoaded method for handle empty initial load

val livedPageList = LivePagedListBuilder(sourceFactory, config)
        .setBoundaryCallback(object: PagedList.BoundaryCallback<YourItem>() {
            override fun onZeroItemsLoaded() {
                super.onZeroItemsLoaded()
                // Handle empty initial load here
            }

            override fun onItemAtEndLoaded(itemAtEnd: YourItem) {
                super.onItemAtEndLoaded(itemAtEnd)
                // Here you can listen to last item on list
            }

            override fun onItemAtFrontLoaded(itemAtFront: YourItem) {
                super.onItemAtFrontLoaded(itemAtFront)
                // Here you can listen to first item on list
            }
        })
        .build()

You can check complete answer here

ysfcyln
  • 2,857
  • 6
  • 34
  • 61