作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我如何才能获得当前的项目数量,即我的 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/
我是一名优秀的程序员,十分优秀!