这是一个电子商务应用程序,我面临与从购物车(添加/删除)商品相关的问题,当我添加两个商品,然后如果我删除其中一个商品时,就会出现问题,总金额未正确更新除非我关闭
MyCartFragment
并再次重新打开它,
这个
MyCartFragment
看起来就像只有一件商品时
首先这个
CartItemModel
类,它有两个伴随对象,一个是 CART_ITEM
用于常规购物车项目,第二个 TOTAL_AMOUNT
用于计算价格和总额
@Parcelize
data class CartItemModel(
var type: Int?,
var productId: String?,
var productImage: String?,
var productName: String?,
var freeCoupons: Long?,
var productPrice: String?,
var cuttedPrice: String?,
var productQuantity: Long?,
var maxQuantity: Long?,
var stockQuantity: Long?,
var offersApply: Long?,
var couponsApplied: Long?,
var inStock: Boolean?,
var qtyIDs: ArrayList<String>?,
var selectedCouponId: String?,
var discountedPrice: String?,
var totalItems: Int?,
var totalItemsPrice: Int?,
var deliveryPrice: String?,
var totalAmount: Int?,
var savedAmount: Int?
) : Parcelable {
//
constructor(type: Int) : this(
type,
productId = null,
productImage = null,
productName = null,
freeCoupons = null,
productPrice = null,
cuttedPrice = null,
productQuantity = null,
maxQuantity = null,
stockQuantity = null,
offersApply = null,
couponsApplied = null,
inStock = null,
qtyIDs = null,
selectedCouponId = null,
discountedPrice = null,
totalItems = null,
totalItemsPrice = null,
deliveryPrice = null,
totalAmount = null,
savedAmount = null
) {
this.type = type
}
// var isInStock = false
////////CART TOTAL
constructor(
type: Int,
totalItems: Int,
totalItemsPrice: Int,
deliveryPrice: String,
totalAmount: Int,
savedAmount: Int
) : this(
type, null, null, null,
null, null, null,
null, null, null,
null, null, null,
null, null, null,
totalItems, totalItemsPrice, deliveryPrice, totalAmount, savedAmount
)
companion object {
const val CART_ITEM = 0
const val TOTAL_AMOUNT = 1
}
}
这部分来自
CartAdapter
类,特别是onCreateViewHolder
和onBindViewHolder
,在onBinViewHolder
的第二部分,当项目是CartItemModel.TOTAL_AMOUNT
时,我计算总项目价格和折扣等
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
CartItemModel.CART_ITEM -> {
val cartItemLayoutBinding: CartItemLayoutBinding = CartItemLayoutBinding.inflate(
LayoutInflater.from(parent.context), parent, false
)
CartItemViewHolder(cartItemLayoutBinding)
}
CartItemModel.TOTAL_AMOUNT -> {
val cartTotalAmountLayoutBinding: CartTotalAmountLayoutBinding =
CartTotalAmountLayoutBinding.inflate(
LayoutInflater.from(parent.context), parent, false
)
CartTotalAmountViewHolder(cartTotalAmountLayoutBinding)
}
else -> {
val cartItemLayoutBinding: CartItemLayoutBinding = CartItemLayoutBinding.inflate(
LayoutInflater.from(parent.context), parent, false
)
CartItemViewHolder(cartItemLayoutBinding)
}
}
}
override fun onBindViewHolder(
holder: RecyclerView.ViewHolder,
@SuppressLint("RecyclerView") position: Int
) {
when (asyncListDiffer.currentList[position].type) {
CartItemModel.CART_ITEM -> {
val cartItemModel: CartItemModel = asyncListDiffer.currentList[position]
(holder as CartItemViewHolder).bind(cartItemModel, position)
if (lastPosition < position) {
val animation = AnimationUtils.loadAnimation(
holder.itemView.context,
R.anim.fade_in
)
holder.itemView.animation = animation
lastPosition = position
}
}
CartItemModel.TOTAL_AMOUNT -> {
var totalItems = 0
var totalItemsPrice = 0
val deliveryPrice: String
val totalAmount: Int
var savedAmount = 0
// var i = 0
for (i in 0 until asyncListDiffer.currentList.size) {
if (asyncListDiffer.currentList[i].type == CartItemModel.CART_ITEM
&& asyncListDiffer.currentList[i].inStock == true) {
val quantity = asyncListDiffer.currentList[i].productQuantity
totalItems = (totalItemsPrice + quantity!!).toInt()
totalItemsPrice += if (asyncListDiffer.currentList[position].selectedCouponId.isNullOrEmpty()) {
asyncListDiffer.currentList[i].productPrice?.toInt()!! * quantity.toInt()
} else {
asyncListDiffer.currentList[i].discountedPrice?.toInt()!! * quantity.toInt()
}
if (asyncListDiffer.currentList[i].cuttedPrice?.isNotEmpty()!!) {
savedAmount += (asyncListDiffer.currentList[i].cuttedPrice?.toInt()!! - asyncListDiffer.currentList[i].productPrice?.toInt()!!) * quantity.toInt()
if (!asyncListDiffer.currentList[position].selectedCouponId.isNullOrEmpty()) {
savedAmount += (asyncListDiffer.currentList[i].productPrice?.toInt()!! - asyncListDiffer.currentList[i].discountedPrice?.toInt()!!) * quantity.toInt()
}
} else {
if (asyncListDiffer.currentList[position].selectedCouponId?.isNotEmpty()!!) {
savedAmount += (asyncListDiffer.currentList[i].productPrice?.toInt()!! - asyncListDiffer.currentList[i].discountedPrice?.toInt()!!) * quantity.toInt()
}
}
}
}
if (totalItemsPrice > 500) {
deliveryPrice = "Free"
totalAmount = totalItemsPrice
} else {
deliveryPrice = "60"
totalAmount = totalItemsPrice + 60
}
asyncListDiffer.currentList[position].totalItems = totalItems
asyncListDiffer.currentList[position].totalItemsPrice = totalItemsPrice
asyncListDiffer.currentList[position].deliveryPrice = deliveryPrice
asyncListDiffer.currentList[position].totalAmount = totalAmount
asyncListDiffer.currentList[position].savedAmount = savedAmount
Log.d(TAG, "onBindViewHolder: totalItems $totalItems")
Log.d(TAG, "onBindViewHolder: totalItems $totalItemsPrice")
Log.d(TAG, "onBindViewHolder: totalItems $deliveryPrice")
Log.d(TAG, "onBindViewHolder: totalItems $totalAmount")
Log.d(TAG, "onBindViewHolder: totalItems $savedAmount")
(holder as CartTotalAmountViewHolder).setTotalAmount(
totalItems, totalItemsPrice, deliveryPrice, totalAmount, savedAmount
)
myCartUtil?.getTotalAmount(totalAmount)
if (lastPosition < position) {
val animation = AnimationUtils.loadAnimation(
holder.itemView.context,
R.anim.fade_in
)
holder.itemView.animation = animation
lastPosition = position
}
}
else -> return
}
}
我猜这是因为您正在项目列表中就地更改数据。 我想这就是问题所在:
asyncListDiffer.currentList[position].totalItems = totalItems
在这种情况下,你的适配器将永远不知道哪些数据被更改以及应该通知什么。
val changedList = asyncListDiffer.currentList.toMutableList()
changedList[position] = asyncListDiffer.currentList[position].copy( totalItems = totalItems,
totalItemsPrice = totalItemsPrice,
deliveryPrice = deliveryPrice,
totalAmount = totalAmount,
savedAmount = savedAmount)
submitList(changedList) // infinite loop here?
我在这里描述了概念上的变化。要使您的应用程序更加稳定和健壮,需要进行多项更改: