gpt4 book ai didi

安卓分页 3 : It is possible to get the itemcount from PagingData?

转载 作者:行者123 更新时间:2023-12-03 10:41:18 24 4
gpt4 key购买 nike

我如何才能获得当前的项目数量,即我的 PagingData<Product>持有?我找到的唯一解决方案是调用 shopListAdapter.itemCount但这总是返回 0。
我要做的是:获取我的 PagingAdapter 当前显示的当前项目数量,将此值提交给我的 shopViewModel,然后在另一个 fragment (DialogFragment)中使用此值。或者:从我的PagingData<Product>获取元素数量在我的 View 模型中
这是我目前的方法
主要 fragment

@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)
}
}
}
对话 fragment
@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
}
}

}
View 模型
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,它将返回不正确的数字)。
要等待适配器呈现页面,最简单的方法是使用 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)
}
}
}

关于安卓分页 3 : It is possible to get the itemcount from PagingData<T>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65461277/

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