Skip to content

Instantly share code, notes, and snippets.

@WLun001
Created May 20, 2018 06:05
Show Gist options
  • Save WLun001/8655d99b14d274ff16cc079790299f3f to your computer and use it in GitHub Desktop.
Save WLun001/8655d99b14d274ff16cc079790299f3f to your computer and use it in GitHub Desktop.
Set OnItemClickListener on RecycleView Android Kotlin
private var listener = object : Adapter.OnItemClickListener {
override fun onItemClick(hospital: DataClassWrapper.MapsHospital) {
getHospitalDetails(hospital)
}
}
private var adapter = Adapter(hospitals, listener)
class Adapter(private var hospitalList: ArrayList<MapsHospital>,
private var listener: OnItemClickListener)
: RecyclerView.Adapter<MapsHospitalAdapter.ViewHolder>() {
interface OnItemClickListener {
fun onItemClick(hospital: MapsHospital)
}
override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
holder?.bindView(hospitalList[position], listener)
}
// override fun getItemCount(): Int {
// return hospitalList.size
// }
// override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
// val v = LayoutInflater.from(parent?.context).inflate(R.layout.custom_list_item, parent, false)
// return ViewHolder(v)
// }
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bindView(hospital: MapsHospital, listener: OnItemClickListener) {
itemView.setOnClickListener { listener.onItemClick(hospital) }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment