Skip to content

Instantly share code, notes, and snippets.

@chipcerio
Created May 18, 2018 03:44
Show Gist options
  • Save chipcerio/1cba79749402207ca7627bfc5b14d8ca to your computer and use it in GitHub Desktop.
Save chipcerio/1cba79749402207ca7627bfc5b14d8ca to your computer and use it in GitHub Desktop.
class MainActivity : Activity, Adapter.OnCellClickListener {
override fun onCreate() {
recyclerView.setAdapter(Adapter(this)) // this refers to OnCellClickListener
}
override fun onCellClick(item: ChattrItem) {
// hoooray i have access to ChattrItem!!!
}
}
class Adapter(private val listener: OnCellClickListener) {
override fun onCreateViewHolder() {...}
override fun onBindViewHolder() {...}
override fun getItemCount() {...}
interface OnCellClickListener {
fun onCellClick(item: ChattrItem)
}
inner class CustomViewHolder(view: View) {
fun bind(item: ChattrItem) {
view.setOnClickListener {
listener.onCellClick(item) // pass ChattrItem to this interface
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment