title: RecyclerView itemClickListener in Kotlin
body: <p>I'm writing my first app in Kotlin after 3 years of experience with Android.
  Just confused as to how to utilize itemClickListener with a RecyclerView in Kotlin.</p>
  
  <p>I have tried the trait (edit: now interface) approach, very Java-like</p>
  
  <pre><code>public class MainActivity : ActionBarActivity() {
  
    protected override fun onCreate(savedInstanceState: Bundle?) {
  
      // set content view etc go above this line
  
      class itemClickListener : ItemClickListener {
        override fun onItemClick(view: View, position: Int) {
          Toast.makeText(this@MainActivity, "TEST: " + position, Toast.LENGTH_SHORT).show()
        }
      }
  
      val adapter = DrawerAdapter(itemClickListener())
      mRecyclerView.setAdapter(adapter)
   }
  
    trait ItemClickListener {
      fun onItemClick(view: View, position: Int)
    }
  }
  </code></pre>
  
  <p>That seemed very redundant so I tried the inner class approach:</p>
  
  <pre><code>inner class ItemClickListener {
      fun onItemClick(view: View, position: Int) {
          startActivityFromFragmentForResult&lt;SelectExerciseActivity&gt;(SELECT_EXERCISES)
      }
  }
  </code></pre>
  
  <p>And then just setting the adapter's click listener like this:</p>
  
  <pre><code>val adapter = WorkoutsAdapter(ItemClickListener())
  </code></pre>
  
  <p>But I'm still not satisfied with this because I think there might be a better, cleaner way. I'm trying to essentially achieve something like this:
  <a href="https://stackoverflow.com/questions/24471109/recyclerview-onclick/26196831#26196831">RecyclerView onClick</a></p>
  
  <p>Any suggestions?</p>
  
  <p><strong>Ended up going with a variation of the approved answer</strong></p>
  
  <p>Defined the function in the activity:</p>
  
  <pre><code>val itemOnClick: (View, Int, Int) -&gt; Unit = { view, position, type -&gt;
      Log.d(TAG, "test")
  }
  </code></pre>
  
  <p>Passed the function itself on to the adapter like this:</p>
  
  <pre><code>class ExercisesAdapter(val itemClickListener: (View, Int, Int) -&gt; Unit) : RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt;() {
  
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        // other stuff up here
        val vhExercise = ExerciseVH(view) // view holder
        // on to the view holder through the extension function
        vhExercise.onClick(itemClickListener)
      }
  }
  </code></pre>
  
  <p>Extension function by Loop in the approved answer below.</p>
  
  <pre><code>fun &lt;T : RecyclerView.ViewHolder&gt; T.onClick(event: (view: View, position: Int, type: Int) -&gt; Unit): T {
      itemView.setOnClickListener {
          event.invoke(it, getAdapterPosition(), getItemViewType())
      }
      return this
  }
  </code></pre>