Skip to content

Paris Integration (Dynamic Styling)

Eli Hart edited this page Apr 18, 2018 · 6 revisions

If you use Paris, Epoxy will generate additional code in your models to support dynamical styling.

How it works

Epoxy recognizes custom views annotated with Paris's @Styleable annotation.

@Styleable
@ModelView
class MyView extends View {
   // View code ommitted
}

If a Styleable view is used to create an Epoxy model (ie it also has a @ModelView annotation) then the generated model will additionally contain these methods:

public MyViewModel_ style(Style style)

public MyViewModel_ styleBuilder(StyleBuilderCallback builder)

These allow you to either set an existing Paris Style object on the model, or use a style builder to dynamically specify styling.

For example:

new MyViewModel_()
  .id("my view)
  .styleBuilder { builder ->
       builder.backgroundRes(R.color.white)
          .alpha(0.5f)
          .paddingBottomDp(12)
  }
}   

Notice that no view holder or layout information needs to be provided to the @ModelView annotation. This is because Epoxy creates the view programmatically and styles it completely with Paris. By default the view's default style is used, per Paris's default rules.

The style information is applied with Paris when the model is bound to a view.

Linking Styles

If the view has any styles linked to it, Epoxy also generates helpers for those.

For example:

@Styleable
@ModelView
class MyView extends View {
   
   // For styles defined in XML
   @Style static final int RED_STYLE = R.style.MyView_Red;

    // For styles defined programmatically
    @Style
    static void greenStyle(MyViewStyleApplier.StyleBuilder builder) {
        builder.background(R.color.green);
    }
}

// Using the linked red style
new MyViewModel_()
  .withRedStyle()

// Using the linked green style
new MyViewModel_()
  .withGreenStyle()

Recycling Views

Styling views with Paris is powerful because it lets us share views with different styles, instead of separating them by view type. This expands our ability to reuse views.

In order to make this work, every style used on a view must specify the same set of attributes. More information can be found in Paris's wiki

Epoxy enforces this at runtime when a model is bound if validation of model usage is enabled in your config. It is highly recommended to enable this for debug builds to catch issues with improper recycling. It does cause significant runtime overhead though, so it should be disabled for prod builds.