How to add a Onclick listener on a RecyclerView in Android Studio with Kotlin?

纵然是瞬间 提交于 2019-12-10 10:58:44

问题


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"


回答1:


  1. Let's define an interface for this use-case:

    interface WorkoutClickLisetner{
       fun onWorkoutClicked(workout: workout)
    }
    
  2. Add WorkoutClickListener as member of CustomAdapter

    var listener : WorkoutClickListener? = null
    

    register click listener for itemViewinside your bindItems method

    itemView.listener = object : View.OnClickListener {
    
      override fun onClick(v: View){
        listener?.onWorkoutClicked(workout)
      }
    }
    
  3. Do not forget to init your WorkoutClickListener where you will delegate action when user clicks the cell:

    val adapter = CustomAdapter(workouts)
    
    adapter.listener = object : WorkoutClickListener { 
        override fun onWorkoutClicked(workout: workout){  
       /*your delegation goes here*/`
     }
    }
    
    recyclerView.adapter = adapter
    



回答2:


use it inside viewHolder

  override fun onClick(view: View) {

    }

if you have specific id's

@OnClick(R.id.id1, R.id.id2)
 fun onClick(view: View) {
        when (view.id) {

            R.id.id1 -> {}
            R.id.id2 -> {}
            else ->{}
       }
}



回答3:


Alright guys I found out how to implement a onclicklistener in Kotlin, here is the link: https://gist.github.com/nesquena/231e356f372f214c4fe6



来源:https://stackoverflow.com/questions/47377929/how-to-add-a-onclick-listener-on-a-recyclerview-in-android-studio-with-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!