Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class SampleAdapter() : RecyclerView.Adapter<SampleAdapter.ViewHolder>() {
var data: List<String> = emptyList()
var onClick: (String) -> Unit = {}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder? {
val binding = ItemListBinding.inflate(LayoutInflater.from(parent.context), parent, false)
binding.root.setOnClickListener {
onClick(data[(parent as RecyclerView).getChildAdapterPosition(it)])
}
return ViewHolder(it)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.binding.text = data[position]
}
overrride fun getItemCount(): Int {
return data.size
}
inner class ViewHolder(val binding: ItemListBinding) : RecyclerView.ViewHolder(binding.root)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment