How do you set OnClickListenrs in your AirBNB Epoxy Models for Android (Kotlin)

391 Views Asked by At

So I'm using AirBNB Epoxy (the view holders version) and I can't seem to find the information where I can set onClickListeners in AirBNB Epoxy. Any information helps, Thanks.

1

There are 1 best solutions below

0
On

According to epoxy's document:

@EpoxyModelClass(layout = R.layout.model_button)
abstract class ButtonModel : EpoxyModelWithHolder<ButtonModel.Holder>() {

    // Declare your model properties like this
    @EpoxyAttribute
    @StringRes
    var text = 0

    @EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
    var clickListener: View.OnClickListener? = null

    override fun bind(holder: Holder) {
        // Implement this to bind the properties to the view
        holder.button.setText(text)
        holder.button.setOnClickListener(clickListener)
    }

    class Holder : EpoxyHolder() {
        lateinit var button: Button
        override fun bindView(itemView: View) {
            button = itemView.findViewById(R.id.button)
        }
    }
}

The generated model should be instantiated directly, and has a setter for each property:

ButtonModel_()
    .id(1)
    .text(R.string.my_text)
    .clickListener { view -> /* do something */ }