我如何在RecyclerView Adapter中获得第二个模型的位置 [英] How can i get the position for second model in RecyclerView Adapter

查看:128
本文介绍了我如何在RecyclerView Adapter中获得第二个模型的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的RecyclerView适配器代码

This is my RecyclerView Adapter code

class RecyclerAdapterMain(

val product: ArrayList<ModelProductMain>,

val viewmodel: ViewModelRoom,

val newitem: List<ModelItemsNew>

) :

RecyclerView.Adapter<RecyclerAdapterMain.ViewHolder>() {


class ViewHolder(itemview: View) : RecyclerView.ViewHolder(itemview) {


    val title: TextView = itemview.product_txt
    val price: TextView = itemview.price_product
    val imageproduct: ImageView = itemview.product_image
    val btn_add_product: Button = itemview.btn_add_product
    val amount_value: TextView = itemview.amount_value
    val button_remove_product: Button = itemview.button_remove_product


}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {


    val layoutview =
        LayoutInflater.from(parent.context).inflate(R.layout.product_items, parent, false)
    return ViewHolder(layoutview)

}

override fun getItemCount(): Int = product.size


override fun onBindViewHolder(holder: ViewHolder, position: Int) {


    val products = product[position]
    holder.title.text = products.title
    holder.price.text = products.price.toString()
    Picasso.get().load(products.image).into(holder.imageproduct)


    holder.itemView.setOnClickListener {

        val bundle = Bundle()
        val myfragment = ItemDetailsfragment()
        myfragment.arguments = bundle


        val activity = it.context as AppCompatActivity
        activity.supportFragmentManager.beginTransaction()
            .replace(R.id.homepage, myfragment)
            .commit()


        bundle.putString("title", products.title)
        bundle.putString("price", products.price.toString())
        bundle.putString("image", products.image.toString())


    }

    holder.amount_value.visibility = View.GONE
    holder.button_remove_product.visibility = View.GONE
    holder.btn_add_product.setOnClickListener {

        holder.amount_value.visibility = View.VISIBLE
        holder.button_remove_product.visibility = View.VISIBLE

            holder.amount_value++    
        
    }

}

}

问题是我想当按钮被按下时会增加产品的数量...为此,我需要每个商品的位置,但需要商品的第一个模型(重写fun getItemCount():Int = product. size)获得了我的新模型所需的位置(val newitem:List).

the problem is I want when the button is pressed the amount of product would be increased ... for this I need the position of each item but the first model of my product (override fun getItemCount(): Int = product.size ) got the position I need position for my new model (val newitem: List ).

我尝试了这段代码val productpos = newitem.position,并给我错误,大小为1,索引也为1 ... 我不知道该如何解决.刚才我需要一个新模型的职位.如何获得新职位?

I tried this code val productpos = newitem.position and give me the error that the size is 1 and the index also is 1 ... I don't know how to fix it . just now that I need the position for a new model. how to get a new position?

推荐答案

尝试一下:

val productpos = newitem.get(position)

,然后获得总大小:

override fun getItemCount(): Int = product.size + newitem.size

这篇关于我如何在RecyclerView Adapter中获得第二个模型的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆