我有一些关于我的项目布局的视图,用于在Kotlin的recyclerView
中显示项目。一些视图将根据条件显示/隐藏。recyclerview中的每个项都正确地显示了视图(button、editText),最后一个项除外。即使最后一项不符合显示视图的标准(它已经隐藏在XML
中),它也会显示这些视图。我已经检查了我的代码,但是我不明白为什么会发生这种情况。
下面的代码在我的ItemListAdapter.kt
类的onBindViewHolder
内。
if (model.category == "Food") {
Log.d("CheckTag", "item is ${model.item_title} and the category is ${model.category}")
holder.binding.tvDeliveryType.visibility = View.VISIBLE
holder.binding.tvDeliveryType.text = "Same Day Delivery"
mFireStore.collection(Constants.CART_ITEMS)
.whereEqualTo(Constants.USER_ID, FirestoreClass().getCurrentUserID())
.whereEqualTo(Constants.PRODUCT_ID, model.product_id).get()
.addOnSuccessListener { document ->
if (document.documents.size > 0) {
holder.binding.llGoToCart.visibility = View.VISIBLE
} else {
holder.binding.llAddToCart.visibility = View.VISIBLE
}
}.addOnFailureListener { e ->
}
holder.binding.btnGoToCart.setOnClickListener {
context.startActivity(Intent(context, CartListActivity::class.java))
}
holder.binding.btnAddToCart.setOnClickListener {
if (holder.binding.etQuantity.text.toString().isNotEmpty()) {
if (holder.binding.etQuantity.text.toString().toInt() > 0) {
holder.binding.spnUom.onItemSelectedListener =
object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
uom = parent?.getItemAtPosition(position).toString()
}
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}
val addToCart = Cart(
FirestoreClass().getCurrentUserID(),
model.user_id,
model.product_id,
model.title,
model.price,
model.image,
holder.binding.etQuantity.text.toString(),
uom = uom
)
FirestoreClass().addToCart(context, addToCart)
Toast.makeText(
context, "Item added to the cart", Toast.LENGTH_SHORT
).show()
holder.binding.llAddToCart.visibility = View.GONE
holder.binding.llGoToCart.visibility = View.VISIBLE
} else {
Toast.makeText(
context, "Please enter a valid quantity", Toast.LENGTH_SHORT
).show()
}
} else {
Toast.makeText(
context, "Please enter quantity required", Toast.LENGTH_SHORT
).show()
}
}
} else {
holder.binding.tvDeliveryType.visibility = View.GONE
}
这是由于代码中缺少else
部分。