Android paging 3: 是否可以从 PagingData<T> 获取 itemcount?

Android paging 3: It is possible to get the itemcount from PagingData<T>?

我如何才能获得我的 PagingData<Product> 持有的当前物品数量?我找到的唯一解决方案是调用 shopListAdapter.itemCount 但这总是 returns 0.

我想做的是:获取我的 PagingAdapter 当前显示的项目的当前数量,将此值提交到我的 shopViewModel,然后在另一个片段 (DialogFragment) 中使用此值。或者:从我的 PagingData<Product> 中获取我的视图模型

中的项目数量

这是我目前的做法

主要片段

@AndroidEntryPoint
class ShopFragment : Fragment(R.layout.fragment_shop), ShopAdapter.OnItemClickListener {
    private val shopViewModel: ShopViewModel by navGraphViewModels(R.id.nav_shop) { defaultViewModelProviderFactory }
    private var _shopBinding: FragmentShopBinding? = null
    private val shopBinding: FragmentShopBinding get() = _shopBinding!!
    @Inject lateinit var shopListAdapter: ShopAdapter

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _shopBinding = FragmentShopBinding.inflate(inflater, container, false)
        return shopBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        collectShopList()
    }

    private fun collectShopList(){
        shopViewModel.shopPagingData.observe(viewLifecycleOwner)  {
            shopListAdapter.submitData(viewLifecycleOwner.lifecycle, it)
            shopViewModel.setItemAmount(shopListAdapter.itemCount)
        }
    }
}

DialogFragment

@AndroidEntryPoint
class ShopFilterFragment : DialogFragment() {
    private val shopViewModel: ShopViewModel by navGraphViewModels(R.id.nav_shop) { defaultViewModelProviderFactory }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_shop_filter, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        shopViewModel.itemAmount.observe(viewLifecycleOwner) {
            toast(it.toString()) <-- Always displays 0
        }
    }

}

视图模型

class ShopViewModel @ViewModelInject constructor(
    private val shopRepository: ShopRepository
) : ViewModel() {
    private val _itemAmount = MutableLiveData<Int>()
    val itemAmount: LiveData<Int> get() = _itemAmount

    private val query = MutableLiveData(QueryHolder(null, null, null))

    val shopPagingData = query.switchMap { query -> shopRepository.search(query).cachedIn(viewModelScope) }

    fun search(newQuery: QueryHolder) {
        query.value = newQuery
    }

    fun setItemAmount(amount: Int) {
        _itemAmount.value = amount
    }
}

adapter.itemCount 是正确的方法,但请注意它有可能与适配器收到的内容竞争(页面将加载然后通知适配器 - 如果您在这些之间调用 .itemCount步骤它将 return 不正确的数字)。

要等待适配器呈现页面,最简单的方法是使用 loadStateListener / loadStateFlow,因为 LoadState 更新保证与适配器事件同步。

shopListAdapter.addLoadStateListener { combinedLoadStates ->
  // If you don't want to call all the time, you
  // can filter on changes in combinedLoadStates
  shopViewModel.setItemAmount(shopListAdapter.itemCount)
}

不要忘记注销以防泄密。

或者,您可以使用基于流的方法:

.. onViewCreated(..) {
  viewLifecycleOwner.lifecycleScope.launch {
    shopListAdapter.loadStateFlow.collect { combinedLoadStates ->
      // If you don't want to call all the time, you
      // can filter on changes in combinedLoadStates
      shopViewModel.setItemAmount(shopListAdapter.itemCount)
    }
  }
}