Skip to content

Data Binding Support

Eli Hart edited this page May 9, 2017 · 21 revisions

(Added In 2.1 Release)

Epoxy supports Android data binding layouts via the DataBindingEpoxyModel class. This class can be subclassed manually or you can let Epoxy automatically create a model for each of your data binding layouts. Data binding models can then be used in an EpoxyController like any other model.

Automatic Model Generation

Epoxy can generate a subclass of DataBindingEpoxyModel for each of your data binding layouts. Simply declare which layouts you want models created for by creating a package-info.java file and annotating it with EpoxyDataBindingLayouts. Then include your layout file names in the annotation.

For example,

@EpoxyDataBindingLayouts({R.layout.header_view})
package com.airbnb.epoxy;

A model named HeaderViewModel_ will be created in the same package as the package-info.java file. This model will have a setter for each of the variables in the layout, and each variable will be bound with when the model is bound. This model incorporates diffing, so that if the model is changed only the changed variables are rebound. In other aspects the generated model works just like a normal EpoxyModel.

The generated model name is created by camel casing the layout file name and appending Model_. The layouts must not specify a custom data binding class name or package via the class="com.example.CustomClassName" override in the layout xml.

Custom Data Binding Models

You may also subclass DataBindingEpoxyModel directly if you want to handle binding manually, although this should rarely be needed.