【问题标题】:How to show empty view with Paging 3 library in Android如何在 Android 中使用 Paging 3 库显示空视图
【发布时间】:2020-09-04 21:21:31
【问题描述】:

我想在 paging3 加载一个空列表时显示空视图。

它似乎适用于以下代码。这是使用 paging 3 库的正确方法吗?:

        adapter?.addLoadStateListener { loadState ->
            adapter?.apply {
                if (itemCount <= 0 && !loadState.source.refresh.endOfPaginationReached) {
                    Timber.d("==> to show empty view")
                    tvEmptyView.isGone = false
                } else {
                    Timber.d("==> to hide empty view")
                    tvEmptyView.isGone = true
                }
            }
        } 

【问题讨论】:

    标签: android android-layout android-paging-3


    【解决方案1】:

    这对我有用:

    if (loadState.source.refresh is LoadState.NotLoading &&
        loadState.append.endOfPaginationReached &&
        adapter.itemCount < 1
    ) {
       recyclerView.isVisible = false
       textViewEmpty.isVisible = true
    } else {
        textViewEmpty.isVisible = false
    }
    

    【讨论】:

    • 你的解决方案和我的一样;但由于保罗首先发布了他的解决方案,我将他的解决方案标记为解决方案。谢谢。
    【解决方案2】:

    可以直接插入适配器loadStateFlow,例如

        lifecycleScope.launchWhenCreated {
            @OptIn(ExperimentalCoroutinesApi::class)
            adapter.loadStateFlow.collectLatest { loadStates ->
                val refresher = loadStates.refresh
                val displayEmptyMessage =  (refresher is LoadState.NotLoading && refresher.endOfPaginationReached && adapter.itemCount == 0)
                layoutBinding.emptyStateMessage.isVisible = displayEmptyMessage
                layoutBinding.emptyStateImage.isVisible = displayEmptyMessage
                layoutBinding.swipeToRefresh.isRefreshing = refresher is LoadState.Loading
            }
        }
    

    【讨论】:

    • 由于某种原因loadStates.refresh.endOfPaginationReached 对我不起作用。我必须使用loadStates.append.endOfPaginationReached (append) 就像@Florian Walther 回答的那样。
    【解决方案3】:

    在我的情况下,如果它们小于 11,我必须使用 concatAdapter 来显示空视图低于正常结果。不幸的是,分页 3 库会给我很多关于 @987654321 的误报结果@ 和 loadState.append.endOfPaginationReached 状态所以我不得不通过引入一个计数器来仔细检查它。 Florian 或 Paul 的答案都可以。

         offersAdapter.addLoadStateListener { updateUiOnNewLoadSate(it) }
        
         private fun updateUiOnNewLoadSate(loadState: CombinedLoadStates) {
        
                Timber.i("STATE $loadState")
              
                // Show empty view if results are less or equal 10
                val displayEmptySearch =
                    loadState.source.refresh is LoadState.NotLoading && loadState.append.endOfPaginationReached && offersAdapter.itemCount <= 10 && loadStatesCounter > 1
                if (displayEmptySearch) {
        //here I display the empty view as a part of the
       //infinite scrolling recyclerview by using concatAdapter
                    concatAdapter.addAdapter(emptyViewAdapter)
                } else {
                    concatAdapter.removeAdapter(emptyViewAdapter)
                }
                loadStatesCounter++ //only apply above block when loadStatesCounter > 1
        }
    

    【讨论】:

    • 尝试添加 adapter.itemCount &lt; 1 以避免在数据从流中加载之前出现误报
    • 很好,弗洛里安这也有点工作,但根据我的逻辑,我不得不像这样 adapter.itemCount &gt; 1 反转运算符,但这引入了另一个问题。当只有一个结果时,它不会显示空视图。所以我暂时保留柜台。
    猜你喜欢
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 1970-01-01
    • 2015-03-28
    相关资源
    最近更新 更多