【问题标题】:Android: ListAdapter.Currentlist.isEmpty() is showing true even tho list is not emptyAndroid:ListAdapter.Currentlist.isEmpty() 显示为真,即使列表不为空
【发布时间】:2020-12-11 22:44:54
【问题描述】:

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

片段

@AndroidEntryPoint
class ShoppingCartFragment(
    private val cacheMapper: ShopCacheMapper
) : Fragment(R.layout.fragment_shopping_cart), ShoppingCartListAdapter.OnItemClickListener {
    private val shoppingCartViewModel: ShoppingCartViewModel by viewModels()
    private val shoppingCartBinding: FragmentShoppingCartBinding by viewBinding()
    @Inject lateinit var shoppingCartAdapter: ShoppingCartListAdapter

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        bindObjects()
        observeList()
        hideBtnIfListEmpty()
        toast("LISTE IST EMPTY: ${shoppingCartAdapter.currentList.isEmpty()}")
    }

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

    private fun bindObjects() = with(shoppingCartBinding) {
        adapter = shoppingCartAdapter.apply { clickHandler(this@ShoppingCartFragment) }
        viewModel = shoppingCartViewModel
        lifecycleOwner = viewLifecycleOwner
    }

    override fun forwardCardClick(productCacheEntity: ProductCacheEntity) {
        val product = cacheMapper.mapFromEntity(productCacheEntity)
        val action = ShoppingCartFragmentDirections.actionShoppingCartFragmentToShopItemFragment(product)
        findNavController().navigate(action)
    }

    override fun fordwardBtnIncreaseClick(id: Int) {
        shoppingCartViewModel.increaseProductQuantityById(id)
    }

    override fun fordwardBtnDecreaseClick(id: Int) {
        shoppingCartViewModel.decreaseProductQuantityById(id)
    }

    override fun forwardBtnDeleteClick(id: Int) {
        shoppingCartViewModel.deleteProductById(id)
    }

    override fun onDestroyView() {
        requireView().findViewById<RecyclerView>(R.id.rv_shopping_cart).adapter = null
        super.onDestroyView()
    }

    // here lies the problem
    private fun hideBtnIfListEmpty() {
        if (shoppingCartAdapter.currentList.isEmpty()) shoppingCartBinding.shoppingCartBtn.root.visibility = View.INVISIBLE
        else if (shoppingCartAdapter.currentList.isNotEmpty()) shoppingCartBinding.shoppingCartBtn.root.visibility = View.VISIBLE
    }

}

列表适配器

@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 (app:recyclerview_adapter_stableID)

@BindingAdapter("recyclerview_adapter_stableID")
fun setRecyclerViewAdapterWithStableId(view: RecyclerView, adapter: RecyclerView.Adapter<*>) {
    view.run {
        this.setHasFixedSize(true)
        this.adapter = adapter
    }
}

【问题讨论】:

    标签: android kotlin android-recyclerview listadapter


    【解决方案1】:

    在确保列表已准备好并由observeList() 提交之前,您太早在hideBtnIfListEmpty() 内调用ListAdapter.Currentlist.isEmpty()

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

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

    【讨论】:

    • 完美运行,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多