gpt4 book ai didi

kotlin - 如何在 kotlin 中单击 recyclerview 项目内的按钮

转载 作者:行者123 更新时间:2023-12-05 05:04:32 26 4
gpt4 key购买 nike

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

该函数用于点击recycler查看item 覆盖乐趣 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 项目中执行点击操作。请帮忙

最佳答案

根据您的代码假设您想要的按钮是 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)
}
}

关于kotlin - 如何在 kotlin 中单击 recyclerview 项目内的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61050849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com