Android:ListAdapter.Currentlist.isEmpty()显示为true,即使列表不是空的

Android:ListAdapter.Currentlist.isEmpty()显示为true,即使列表不是空的,android,kotlin,android-recyclerview,listadapter,Android,Kotlin,Android Recyclerview,Listadapter,我的问题是,当我编写MyListAdapter.currentList.isEmpty()时,值是true即使列表不是空的!(我可以在我的视图和数据库中看到它)。我做错了什么,为什么值为真即使它应该是为假 碎片 @AndroidEntryPoint 类ShoppingCartFragment( 私有val缓存映射器:ShopCacheMapper ):Fragment(R.layout.Fragment\u shopping\u cart),ShoppingCartListAdapter.OnI

我的问题是,当我编写
MyListAdapter.currentList.isEmpty()
时,值是
true
即使列表不是空的!(我可以在我的视图和数据库中看到它)。我做错了什么,为什么值
为真
即使它应该是
为假

碎片
@AndroidEntryPoint
类ShoppingCartFragment(
私有val缓存映射器:ShopCacheMapper
):Fragment(R.layout.Fragment\u shopping\u cart),ShoppingCartListAdapter.OnItemClickListener{
private val shoppingCartViewModel:shoppingCartViewModel by viewModels()
private val shoppingCartBinding:FragmentShoppingCartBinding by viewBinding()
@注入lateinit var shoppingCartAdapter:ShoppingCartListAdapter
覆盖已创建的视图(视图:视图,保存状态:捆绑?){
super.onViewCreated(视图,savedInstanceState)
bindObjects()
观察家
hideBtnIfListEmpty()
toast(“列表为空:${shoppingCartAdapter.currentList.isEmpty()}”)
}
私人娱乐观察家(){
shoppingCartViewModel.productList.observe(viewLifecycleOwner){
shoppingCartAdapter.submitList(it)
}
}
private fun bindObjects()=带(shoppingCartBinding){
adapter=shoppingCartAdapter.apply{clickHandler(this@ShoppingCartFragment) }
viewModel=购物车viewModel
lifecycleOwner=视图lifecycleOwner
}
重写CardClick(productCacheEntity:productCacheEntity){
val product=cacheMapper.mapFromEntity(productCacheEntity)
val action=ShoppingCartFragmentDirections.actionShoppingCartFragmentToShopItemFragment(产品)
findNavController()。导航(操作)
}
覆盖fun fordwardBtnIncreaseClick(id:Int){
shoppingCartViewModel.increaseProductQuantityById(id)
}
覆盖FordWardBtnedRegressClick(id:Int){
shoppingCartViewModel.decreaseProductQuantityById(id)
}
覆盖转发BTNDeleteck(id:Int){
shoppingCartViewModel.deleteProductById(id)
}
重写onDestroyView(){
requireView().findViewById(R.id.rv\u购物车)。适配器=null
super.onDestroyView()
}
//问题就在这里
私人娱乐隐藏区{
if(shoppingCartAdapter.currentList.isEmpty())shoppingCartBinding.shoppingCartBtn.root.visibility=View.INVISIBLE
else if(shoppingCartAdapter.currentList.isNotEmpty())shoppingCartBinding.shoppingCartBtn.root.visibility=View.visibility
}
}
列表适配器
@FragmentScoped
类ShoppingCartListAdapter@Inject构造函数():ListAdapter(配套){
私有lateinit变量clickListener:OnItemClickListener
伴随对象:DiffUtil.ItemCallback(){
覆盖所有项目相同(oldItem:ProductCacheEntity,newItem:ProductCacheEntity):Boolean=oldItem.id==newItem.id
覆盖相同的内容(oldItem:ProductCacheEntity,newItem:ProductCacheEntity):布尔值=oldItem==newItem
}
内部类ProductViewHolder(val绑定:ShoppingCartListItemBinding):RecyclerView.ViewHolder(binding.root)
重写CreateViewHolder(父级:ViewGroup,viewType:Int):ProductViewHolder{
val layoutInflater=layoutInflater.from(parent.context)
val binding=ShoppingCartListItemBinding.inflate(LayoutFlater,parent,false)
返回ProductViewHolder(绑定)。另外{
有(约束力){
ivProduct.setOnClickListener{clickListener.forwardCardClick(binding.product!!)}
tvTitle.setOnClickListener{clickListener.forwardCardClick(binding.product!!)}
btnIncrease.setOnClickListener{clickListener.forwardbtnincreaceclick(binding.product!!.id)}
btnderence.setOnClickListener{clickListener.forwardbtnderenceclick(binding.product!!.id)}
btnDelete.setOnClickListener{clickListener.forwardBtnDeleteClick(binding.product!!.id)}
}
}
}
覆盖BindViewHolder(holder:ProductViewHolder,位置:Int){
val currentProduct=getItem(位置)?:返回
holder.binding.product=当前产品
holder.binding.BTNDEREACE.isEnabled=currentProduct.quantity>1
holder.binding.executePendingBindings()文件
}
接口侦听器{
趣味forwardCardClick(productCacheEntity:productCacheEntity)
fun ForwardBtnIncremeseClick(id:Int)
乐趣ForwardBtnedecreaseClick(id:Int)
fun forwardBtnDeleteClick(id:Int)
}
有趣的clickHandler(clickEventHandler:OnItemClickListener){
this.clickListener=clickEventHandler
}
}
布局

Bindingadapter(应用程序:recyclerview\u adapter\u stableID)
@BindingAdapter(“recyclerview\u adapter\u stableID”)
fun SetRecycleServiceAdapterWithTableId(视图:RecycleView,适配器:RecycleView.adapter){
view.run{
this.setHasFixedSize(true)
this.adapter=适配器
}
}

在确保列表已准备就绪并由
observeList()提交之前,在
hideBtnIfListEmpty()中调用
ListAdapter.Currentlist.isEmpty()
太早了

您可以在将列表提交到适配器后调用它:

private fun observeList() {
    shoppingCartViewModel.productList.observe(viewLifecycleOwner) {
        shoppingCartAdapter.submitList(it)
        hideBtnIfListEmpty() 
    }
}

它工作得很好,非常感谢!
@FragmentScoped
class ShoppingCartListAdapter @Inject constructor() : ListAdapter<ProductCacheEntity, ShoppingCartListAdapter.ProductViewHolder>(Companion) {
    private lateinit var clickListener: OnItemClickListener

    companion object: DiffUtil.ItemCallback<ProductCacheEntity>() {
        override fun areItemsTheSame(oldItem: ProductCacheEntity, newItem: ProductCacheEntity): Boolean = oldItem.id == newItem.id
        override fun areContentsTheSame(oldItem: ProductCacheEntity, newItem: ProductCacheEntity): Boolean = oldItem == newItem
    }

    inner class ProductViewHolder(val binding: ShoppingCartListItemBinding): RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val binding = ShoppingCartListItemBinding.inflate(layoutInflater, parent, false)
        return ProductViewHolder(binding).also {
            with(binding) {
                ivProduct.setOnClickListener { clickListener.forwardCardClick(binding.product!!) }
                tvTitle.setOnClickListener { clickListener.forwardCardClick(binding.product!!) }
                btnIncrease.setOnClickListener { clickListener.fordwardBtnIncreaseClick(binding.product!!.id) }
                btnDecrease.setOnClickListener { clickListener.fordwardBtnDecreaseClick(binding.product!!.id) }
                btnDelete.setOnClickListener { clickListener.forwardBtnDeleteClick(binding.product!!.id) }
            }
        }
    }

    override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
        val currentProduct = getItem(position) ?: return
        holder.binding.product = currentProduct
        holder.binding.btnDecrease.isEnabled = currentProduct.quantity > 1
        holder.binding.executePendingBindings()
    }

    interface OnItemClickListener {
        fun forwardCardClick(productCacheEntity: ProductCacheEntity)
        fun fordwardBtnIncreaseClick(id: Int)
        fun fordwardBtnDecreaseClick(id: Int)
        fun forwardBtnDeleteClick(id: Int)
    }

    fun clickHandler(clickEventHandler: OnItemClickListener) {
        this.clickListener = clickEventHandler
    }
}
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv_shopping_cart"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    app:layout_constraintBottom_toTopOf="@id/shopping_cart_btn"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/headline"
    app:recyclerview_adapter_stableID="@{adapter}"
    tools:listitem="@layout/shopping_cart_list_item" />
@BindingAdapter("recyclerview_adapter_stableID")
fun setRecyclerViewAdapterWithStableId(view: RecyclerView, adapter: RecyclerView.Adapter<*>) {
    view.run {
        this.setHasFixedSize(true)
        this.adapter = adapter
    }
}
private fun observeList() {
    shoppingCartViewModel.productList.observe(viewLifecycleOwner) {
        shoppingCartAdapter.submitList(it)
        hideBtnIfListEmpty() 
    }
}