当我从响应中得到库存计数为0时,产品不显示,但产品位置显示网格回收器视图kotlin中的空白空间。我将整个列表传递给适配器,然后根据库存计数进行过滤。如果库存计数为0,则不会显示产品,但在应用程序中,它会显示空白位置
class ProductListAdapter(
val productlists: List<Data>,
val context: Context,
val addCart: AddCartListener
) : RecyclerView.Adapter<ProductListAdapter.ProductListViewHolder>() {
private val TAG = "ProductListAdapter"
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductListViewHolder {
var itemView =
LayoutInflater.from(parent.context).inflate(R.layout.product_list, parent, false)
return ProductListViewHolder(itemView)
}
override fun onBindViewHolder(holder: ProductListViewHolder, position: Int) {
val product: Data = productlists[position]
val variantslist: List<Variant> = product.variants
holder.setIsRecyclable(false)
holder.productname.text = product.name
Glide.with(holder.itemView.context)
.load(product.image)
.into(holder.productimage)
Log.e(TAG, "onBindViewHolder: " + product.variants[0].cart_count)
if (variantslist.get(0).cart_count == 0) {
holder.btn_addcart.visibility = View.VISIBLE
holder.ll_cart.visibility = View.GONE
} else {
holder.btn_addcart.visibility = View.GONE
holder.ll_cart.visibility = View.VISIBLE
holder.tv_cart.text = variantslist.get(0).cart_count.toString()
}
holder.btn_addcart.setOnClickListener {
variantslist.get(0).cart_count = variantslist.get(0).cart_count + 1
addCart.addCart(variantslist.get(0))
notifyDataSetChanged()
}
holder.tv_addcart.setOnClickListener {
variantslist.get(0).cart_count = variantslist.get(0).cart_count + 1
addCart.addCart(variantslist.get(0))
notifyDataSetChanged()
}
holder.tv_removecart.setOnClickListener {
variantslist.get(0).cart_count = variantslist.get(0).cart_count - 1
addCart.addCart(variantslist.get(0))
notifyDataSetChanged()
}
if (variantslist[0].stock.toInt() > 0) {
holder.mrp.text = "₹" + "%.2f".format(variantslist[0].price.toDouble())
holder.mrp.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
holder.discountprice.text =
"₹" + "%.2f".format(variantslist[0].discounted_price.toDouble())
if (variantslist[0].measurement_unit_name == null) {
holder.singlevariant.text = variantslist[0].measurement + " pcs"
holder.multivariant.text = variantslist[0].measurement + " pcs"
} else {
holder.singlevariant.text =
variantslist[0].measurement + variantslist[0].measurement_unit_name
holder.multivariant.text =
variantslist[0].measurement + variantslist[0].measurement_unit_name
}
} else {
holder.singlevariant.visibility = View.GONE
holder.multivariant.visibility = View.GONE
holder.mrp.visibility = View.GONE
holder.discountprice.visibility = View.GONE
holder.productCardView.visibility = View.GONE
holder.ProductConstraint.visibility = View.GONE
holder.productimage.visibility = View.GONE
holder.productname.visibility = View.GONE
holder.btn_addcart.visibility = View.GONE
holder.ll_cart.visibility = View.GONE
holder.tv_addcart.visibility = View.GONE
holder.tv_removecart.visibility = View.GONE
holder.tv_cart.visibility = View.GONE
holder.pricediff.visibility = View.GONE
holder.pricedifference.visibility = View.GONE
}
var diffvalue =
((variantslist[0].price.toDouble() - variantslist[0].discounted_price.toDouble()))
var percentage = (diffvalue / variantslist[0].price.toDouble()) * 100
if (percentage > 3) {
holder.pricediff.visibility = View.VISIBLE
holder.pricedifference.visibility = View.VISIBLE
holder.pricediff.text = percentage.toInt().toString() + "% nOff"
} else {
holder.pricediff.visibility = View.GONE
holder.pricedifference.visibility = View.GONE
}
if (variantslist.size > 1) {
holder.multivariant.visibility = View.VISIBLE
holder.singlevariant.visibility = View.GONE
holder.multivariant.setOnClickListener {
//need to show dropown
showCustomDialog(product)
}
} else {
//navigate product details page
holder.singlevariant.visibility = View.VISIBLE
holder.multivariant.visibility = View.GONE
}
holder.itemView.setOnClickListener {
movefrag(product)
}
}
open fun getUserId(): String? {
val preferences = context.getSharedPreferences(
"userInfo",
Activity.MODE_PRIVATE
) //Frequent to get SharedPreferences need to add a step getActivity () method
return preferences.getString("customer_id", "90336")
}
fun movefrag(product: Data) {
addCart.navFragment(product)
}
fun showCustomDialog(product: Data) {
val dialog = Dialog(context)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(false)
dialog.setContentView(R.layout.dialog_listview)
val btndialog: Button = dialog.findViewById(R.id.btndialog) as Button
btndialog.setOnClickListener { dialog.dismiss() }
val listView: ListView = dialog.findViewById(R.id.listview) as ListView
val adapter = VariantAdapter(context, product, addCart)
listView.adapter = adapter
listView.setOnItemClickListener(AdapterView.OnItemClickListener { parent, view, position, id ->
Log.e(TAG, "showCustomDialog: " + adapter.getItem(position))
dialog.dismiss()
})
dialog.setCanceledOnTouchOutside(true)
dialog.show()
}
override fun getItemCount(): Int {
return productlists.size
}
class ProductListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var productimage: ImageView
var productname: TextView
var mrp: TextView
var discountprice: TextView
var btn_addcart: TextView
var ll_cart: LinearLayout
var tv_addcart: TextView
var tv_removecart: TextView
var tv_cart: TextView
var multivariant: TextView
var singlevariant: TextView
var pricediff: TextView
var pricedifference: ImageView
var productCardView: CardView
var ProductConstraint: ConstraintLayout
init {
productimage = itemView.productimage
productname = itemView.productname
mrp = itemView.mrp
discountprice = itemView.Discountprice
btn_addcart = itemView.btn_addcart
ll_cart = itemView.ll_cart
tv_addcart = itemView.tv_addcart
tv_removecart = itemView.tv_removecart
tv_cart = itemView.tv_cart
multivariant = itemView.multivariant
singlevariant = itemView.singlevariant
pricediff = itemView.pricediff
pricedifference = itemView.pricedifference
productCardView = itemView.productcardview
ProductConstraint = itemView.productconstraint
}
}
}
这是bestsellers的图片,我不应该在条目之间留下空白
这是片段代码
private fun getpopular() {
LoadingUtils.showDialog(context, false)
username = getUserId()
val retrofitBuilder = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_L)
.build()
.create(ApiInterface::class.java)
val retrofitData = retrofitBuilder.getpopularproductitems(90336, username!!)
retrofitData.enqueue(object : Callback<productlist> {
override fun onResponse(call: Call<productlist>, response: Response<productlist>) {
Log.e("latest", "res-" + response.body())
val responsebody: productlist = response.body()!!
LoadingUtils.hideDialog()
popularadapter = popularproductAdapter(frag, responsebody.data, addCart)
popularadapter.notifyDataSetChanged()
linearLayoutManager = LinearLayoutManager(context)
popularproductsRView.adapter = popularadapter
}
override fun onFailure(call: Call<productlist>, t: Throwable) {
Log.d("MainError", "onFailure: " + t.message)
LoadingUtils.hideDialog()
}
})
}
我考虑productlist
类如下:
data class productlist(val uid: Int, val data: List<BasicTimer>,...)
您的Data
类如下:
data class Data(val uid: Int, var stock_count: Int,...)
首先更改productlist
类中变量data
的名称,因为它是Kotlin的关键字。
现在在retrofitData.enqueue()
过滤器列表中确实有stock_count > 0
替换以下行:
popularadapter = popularproductAdapter(frag, responsebody.data, addCart)
:
popularadapter = popularproductAdapter(frag, responsebody.data.filter{it.stock_count > 0}, addCart)
这将不会发送具有stock_count = 0
的对象到RecyclerView进行渲染。
更好的选择是使用ViewModel和RecyclerView,这样你就可以观察数据在初始运行后是否发生了变化。