android - 我如何在RecyclerView Adapter中获得第二个模型的位置

标签 android android-studio kotlin android-recyclerview

这是我的RecyclerView适配器代码

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)。
我试过这段代码val productpos = newitem.position,并给我错误,大小为1,索引也为1 ...
我不知道该如何解决。刚才我需要一个新模型的职位。如何获得新职位?

最佳答案

试试这个:

val productpos = newitem.get(position)
然后得到总大小:
override fun getItemCount(): Int = product.size + newitem.size

关于android - 我如何在RecyclerView Adapter中获得第二个模型的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63552637/

相关文章:

kotlin - 基于条件在 Kotlin 中创建列表

java - 翻新 onFailure 在滚动无限滚动监听器时抛出 "Unexpected end of stream"消息

android - Activity UI 元素未显示

java - DisplayMessageActivity 无法解析为类型构建第一个 android 应用程序

android - Android API和Gradle版本

java - 如果值为零或 "",是否可以将我的 EditText 设置为 Invisible ?

android - 在 Android 上使用 gRPC 处理文件下载

android - 将Alpha最高设置为0%,最低设置为70%-Android-Kotlin

android - 如何从一个 fragment 移动到新 fragment ?

Android + 如何向 ScrollView 或 TextView 添加垂直 "Auto-Scroll"功能