this is my current ViewHolder code, which I would like to modify to let me access the elements throught the binding variable:
class MenuViewHolder(
parent: ViewGroup,
private val onClick: (BaselineLink) -> Unit
) : RecyclerView.ViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.list_item_menu, parent, false)
) {
private val textTitle: AppCompatTextView = itemView.findViewById(R.id.text_title)
The result I want in the last line is
private val textTitle: AppCompatTextView = binding.textTitle
I know I need to modify the line:
LayoutInflater.from(parent.context).inflate(R.layout.list_item_menu, parent, false)
But if I change it as
val inflater = LayoutInflater.from(parent.context)
val binding = PromotionItemBinding.inflate(inflater)
It doesn't work.
You can't declare properties in the super-constructor call.
There are three ways to do this:
class MenuViewHolder(
val binding: ListItemMenuBinding,
private val onClick: (BaselineLink) -> Unit
) : RecyclerView.ViewHolder(binding.root) {
private val textTitle: AppCompatTextView = binding.textTitle
}
// In onCreateViewHolder:
return MenuViewHolder(ListItemMenuBinding.inflate(LayoutInflator.from(parent), parent, false) {
// your click listener
}
class MenuViewHolder private constructor(
val binding: ListItemMenuBinding,
private val onClick: (BaselineLink) -> Unit
) : RecyclerView.ViewHolder(binding.root) {
constructor(
parent: ViewGroup,
onClick: (BaselineLink) -> Unit
): this(
ListItemMenuBinding.inflate(LayoutInflater.from(parent.context), parent, false),
onClick
)
private val textTitle: AppCompatTextView = binding.textTitle
}
bind
it to the existing instance that you have already inflated:class MenuViewHolder(
parent: ViewGroup,
private val onClick: (BaselineLink) -> Unit
) : RecyclerView.ViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.list_item_menu, parent, false)
) {
private val binding = ListItemMenuBinding.bind(itemView)
private val textTitle: AppCompatTextView = binding.textTitle
}