Skip to content

Instantly share code, notes, and snippets.

@chibatching
Created July 12, 2016 06:57
Show Gist options
  • Save chibatching/b0ec373483717db368dcca6899c0d685 to your computer and use it in GitHub Desktop.
Save chibatching/b0ec373483717db368dcca6899c0d685 to your computer and use it in GitHub Desktop.
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