LayoutSpec

annotation class LayoutSpec

Deprecated

A class that is annotated with this annotation will be used to create a composite component that is made up of other components. A layout spec is the logical equivalent of a composite view in Android.

The class annotated with LayoutSpec must implement a method with the or OnCreateLayoutWithSizeSpec annotation. It may also implement methods with the following annotations:

Example:

@LayoutSpec
public class CounterSpec {

 @OnCreateLayout
  static Component onCreateLayout(
    ComponentContext c,
   @Prop int id,
   @State int count) {

    return Row.create(c)
      .backgroundColor(Color.WHITE)
      .heightDip(64)
      .paddingDip(YogaEdge.ALL, 8)
      .child(
        Text.create(c)
          .text(" + ")
          .clickHandler(Counter.onClick(c))
      )
      .child(
        Text.create(c)
          .text(String.valueOf(count))
      )
      .build();
  }

 @OnCreateInitialState
  static void onCreateInitialState(
      ComponentContext c,
      StateValue<Integer> count) {
    count.set(0);
  }

 @OnEvent(ClickEvent.class)
  static void onClick(ComponentContext c, @Prop int id) {
    Counter.increment(c, id);
  }

 @OnUpdateState
  static void increment(StateValue<Integer> count, @Param int counterId) {
    count.set(count.get() + 1);
  }
}
layout spec flow chart

If you want to create a component that mounts its own content, then use MountSpec instead. See more docs at https://fblitho.com

Deprecated

Use com.facebook.litho.KComponents for new derived components instead of LayoutSpecs. Specs should only be used in Java.

If you're new to Kotlin API, read Migrating to the Kotlin API docs.

If you're familiar with it, you can go straight to the APIs matching cheatsheet.

See also

Functions

Link copied to clipboard
abstract fun annotationType(): Class<out Annotation>
Link copied to clipboard
abstract fun equals(p: Any): Boolean
Link copied to clipboard
abstract fun events(): Array<Class<out Any>>
Link copied to clipboard
abstract fun hashCode(): Int
Link copied to clipboard
abstract fun isPublic(): Boolean
Link copied to clipboard
abstract fun simpleNameDelegate(): String
Link copied to clipboard
abstract fun toString(): String
Link copied to clipboard
abstract fun triggers(): Array<Class<out Any>>
Link copied to clipboard
abstract fun value(): String
Class name of the generated component.