2

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"

KrisKodira
  • 53
  • 11

3 Answers3

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
    
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Lukas
  • 1,216
  • 12
  • 25
  • Where does the interface go? and the `var listener : WorkoutClickListener?` is red because it must be initialized. Also in the MainActivity this doesnt seem to be working because of the arrow `adapter.listener = (workout) -> openAnotherActivity(workout);` – KrisKodira Nov 19 '17 at 14:42
  • sorry, I assumed you are using Java8 lambdas support :P I edited the answer – Lukas Nov 19 '17 at 14:50
  • thats nice of you! but where do I put the interface? does it go in my mainactivity? – KrisKodira Nov 19 '17 at 14:57
  • also the `v` is a unresolved reference in my bindItems method – KrisKodira Nov 19 '17 at 15:00
  • Interface should be stand-alone, like your activity(in separare file) i've edited answer, does this snippet was helpful? – Lukas Nov 19 '17 at 15:24
  • there is a type mismatch here with workout: `val adapter = CustomAdapter(workouts)` and here in binditems the listener is a unresolved reference `itemView.listener = View.OnClickListener { listener?.onWorkoutClicked(workout) } ` – KrisKodira Nov 19 '17 at 15:34
0

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 ->{}
       }
}
Pranav Ashok
  • 581
  • 1
  • 6
  • 18
-2

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

KrisKodira
  • 53
  • 11