【问题标题】:How to click a button inside a recyclerview item in kotlin如何单击kotlin中recyclerview项目内的按钮
【发布时间】:2020-07-17 22:08:58
【问题描述】:

我在我的回收站视图项目中放置了一个按钮,但我无法在单击该按钮时执行任何操作,我知道如何单击回收站视图项目,但我不知道如何单击项目中的特定元素回收站视图

该函数用于点击recycler view item 覆盖 fun onCardClick(item: PacketModel, position: Int) {

        lateinit var front_anim: AnimatorSet
        lateinit var back_anim: AnimatorSet


        val scale = this.resources.displayMetrics.density
        packetRecView.cameraDistance = 8000 * scale
        packetRecView.cameraDistance = 8000 * scale





        front_anim =
            AnimatorInflater.loadAnimator(context, R.animator.front_animator) as AnimatorSet
        back_anim = AnimatorInflater.loadAnimator(context, R.animator.back_animator) as AnimatorSet

        if (isFront) {
            front_anim.setTarget(flip.cardOnFront)
            back_anim.setTarget(flip.backCard)
            front_anim.start()
            back_anim.start()
            isFront = false

        } else {
            front_anim.setTarget(flip.backCard)
            back_anim.setTarget(flip.cardOnFront)
            back_anim.start()
            front_anim.start()
            isFront = true


        }




        Toast.makeText(context, item.drugs, Toast.LENGTH_SHORT).show()
    }

这是我的适配器

class PacketAdapter (val packetList: ArrayList<PacketModel> , var clickListener2: onPacketItemClickListener): RecyclerView.Adapter<PacketAdapter.ViewHolder>(){


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

        val a = LayoutInflater.from(parent?.context).inflate(R.layout.packet, parent, false)




        return ViewHolder(a)


    }

    override fun getItemCount(): Int {


        return packetList.size

    }

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

        val packet : PacketModel = packetList[position]

        holder.intialize(packet, clickListener2)


    }


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

     //   val takenButton = itemView.findViewById<Button>(R.id.taken)

        val fCard = itemView.findViewById<CardView>(R.id.cardOnFront)
        val packetTime = itemView.findViewById<TextView>(R.id.timeofPacket)
        val timeMessage = itemView.findViewById<TextView>(R.id.messageofTime)
        val bCars = itemView.findViewById<CardView>(R.id.backCard)
        val drugs = itemView.findViewById<TextView>(R.id.drugs)
        val note = itemView.findViewById<TextView>(R.id.note)
        val dosage = itemView.findViewById<TextView>(R.id.dosage)






        fun intialize(item: PacketModel, action: onPacketItemClickListener){




//            val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ", Locale.getDefault())
//            formatter.timeZone = TimeZone.getTimeZone("UTC")
//            val result = formatter.parse(dateAsString)
//
//
//



//            takenButton.setOnClickListener(this)


            var date = (((item.date) as Timestamp).seconds) * 1000L
            fun convertLongToTime(time: Long): String {
                val date = Date(time)
                val format = SimpleDateFormat("yyyy.MM.dd HH:mm")
                format.timeZone = TimeZone.getTimeZone("GMT+5")

                return format.format(date)
            }
            var convertedDate = convertLongToTime(date)
            packetTime.text = convertedDate
            timeMessage.text = ""
            drugs.text = item.drugs
            note.text  = item.notes
            dosage.text = item.dosage

            itemView.setOnClickListener {
                action.onCardClick(item, adapterPosition)
            }



        }
    }




    interface onPacketItemClickListener{
        fun onCardClick (item: PacketModel, position: Int)
    }


}

现在我想在 recyclerview 项目中执行点击操作。请帮忙

【问题讨论】:

    标签: kotlin android-recyclerview


    【解决方案1】:

    从您的代码中假设您想要的按钮是myButton,并且您将它定义为您的适配器,例如:

        val myButton = itemView.findViewById<Button>(R.id.myButton) // This is your Button, you declared in your xml file.
        val fCard = itemView.findViewById<CardView>(R.id.cardOnFront)
        val packetTime = itemView.findViewById<TextView>(R.id.timeofPacket)
        val timeMessage = itemView.findViewById<TextView>(R.id.messageofTime)
        val bCars = itemView.findViewById<CardView>(R.id.backCard)
        val drugs = itemView.findViewById<TextView>(R.id.drugs)
        val note = itemView.findViewById<TextView>(R.id.note)
        val dosage = itemView.findViewById<TextView>(R.id.dosage)
    

    然后只需在适配器中的 initialize 函数中调用 myButton.setOnClickListener,如下所示:

        fun intialize(item: PacketModel, action: onPacketItemClickListener) {
            var date = (((item.date) as Timestamp).seconds) * 1000L
            fun convertLongToTime(time: Long): String {
                val date = Date(time)
                val format = SimpleDateFormat("yyyy.MM.dd HH:mm")
                format.timeZone = TimeZone.getTimeZone("GMT+5")
    
                return format.format(date)
            }
            var convertedDate = convertLongToTime(date)
            packetTime.text = convertedDate
            timeMessage.text = ""
            drugs.text = item.drugs
            note.text  = item.notes
            dosage.text = item.dosage
    
            itemView.setOnClickListener {
                action.onCardClick(item, adapterPosition)
            }
    
            myButton.setOnClickListener {
                // Do whatever you want on your button click as like you did to your recycler-view item click 
                //action.onCardClick(item, adapterPosition)
            }
        }
    

    【讨论】:

    猜你喜欢
    • 2020-07-29
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多