如何使用Kotlin在Android Studio中的RecyclerView上添加Onclick侦听器? [英] How to add a Onclick listener on a RecyclerView in Android Studio with Kotlin?
问题描述
所以我想在Android中为RecyclerView
设置一个onclicklistener
,但是我不确定该怎么做.
我的Workoutlist
有一个CustomAdapterClass
,如下所示:
class CustomAdapter(val workoutList: ArrayList<workout>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.list_layout, parent, false)
return ViewHolder(v)
}
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.bindItems(workoutList[position])
}
override fun getItemCount(): Int {
return workoutList.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(workout: workout) {
val cardDate = itemView.findViewById<TextView>(R.id.cardDate)
val cardDescription = itemView.findViewById<TextView>(R.id.cardDescription)
cardDate.text = workout.date
cardDescription.text = workout.description
}
}
}
然后像这样(在我的Main Activity
中)进行锻炼:
val recyclerView = findViewById<RecyclerView>(R.id.RecyclerView)
recyclerView.layoutManager = GridLayoutManager(this, 3)
val workouts = ArrayList<workout>()
workouts.add(workout("12.09.2018", "Kniebeugen und Bizeps + Rücken"))
workouts.add(workout("12.09.2018", "Kniebeugen und Bizeps + Rücken"))
workouts.add(workout("12.09.2018", "Kniebeugen und Bizeps + Rücken"))
workouts.add(workout("12.09.2018", "Kniebeugen und Bizeps + Rücken"))
workouts.add(workout("12.09.2018", "Kniebeugen und Bizeps + Rücken"))
workouts.add(workout("12.09.2018", "Kniebeugen und Bizeps + Rücken"))
workouts.add(workout("12.09.2018", "Kniebeugen und Bizeps + Rücken"))
workouts.add(workout("12.09.2018", "Kniebeugen und Bizeps + Rücken"))
val adapter = CustomAdapter(workouts)
recyclerView.adapter = adapter
我想让我的锻炼可点击,因此我可以使用锻炼的名称和说明以他们为目的打开一个新活动.
感谢您的帮助!
我不知道他将扩展名放在重复项"中的位置
好的,我发现了如何在Kotlin中实现onclicklistener, 这是链接: https://gist.github.com/nesquena/231e356f372f214c4fe6 >
so I want to have a onclicklistener
for my RecyclerView
in Android but I am not sure how I should do that.
I have a CustomAdapterClass
for my Workoutlist
that looks like this:
class CustomAdapter(val workoutList: ArrayList<workout>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.list_layout, parent, false)
return ViewHolder(v)
}
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.bindItems(workoutList[position])
}
override fun getItemCount(): Int {
return workoutList.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(workout: workout) {
val cardDate = itemView.findViewById<TextView>(R.id.cardDate)
val cardDescription = itemView.findViewById<TextView>(R.id.cardDescription)
cardDate.text = workout.date
cardDescription.text = workout.description
}
}
}
And I push my workouts in like this (in my Main Activity
):
val recyclerView = findViewById<RecyclerView>(R.id.RecyclerView)
recyclerView.layoutManager = GridLayoutManager(this, 3)
val workouts = ArrayList<workout>()
workouts.add(workout("12.09.2018", "Kniebeugen und Bizeps + Rücken"))
workouts.add(workout("12.09.2018", "Kniebeugen und Bizeps + Rücken"))
workouts.add(workout("12.09.2018", "Kniebeugen und Bizeps + Rücken"))
workouts.add(workout("12.09.2018", "Kniebeugen und Bizeps + Rücken"))
workouts.add(workout("12.09.2018", "Kniebeugen und Bizeps + Rücken"))
workouts.add(workout("12.09.2018", "Kniebeugen und Bizeps + Rücken"))
workouts.add(workout("12.09.2018", "Kniebeugen und Bizeps + Rücken"))
workouts.add(workout("12.09.2018", "Kniebeugen und Bizeps + Rücken"))
val adapter = CustomAdapter(workouts)
recyclerView.adapter = adapter
I would like to make my workouts clickable and so I can use the name and the description from the workout to open a new activity with them as the intention.
Any help is appreciated!
I do not get where he put that extension in the "duplicate"
Alright guys I found out how to implement a onclicklistener in Kotlin, here is the link: https://gist.github.com/nesquena/231e356f372f214c4fe6
这篇关于如何使用Kotlin在Android Studio中的RecyclerView上添加Onclick侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!