This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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