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:
- OnCreateInitialState
- OnCreateTreeProp
- OnCreateTransition
- OnUpdateState
- OnEvent
- OnLoadStyle
- OnEnteredRange
- OnExitedRange
- OnRegisterRanges
- OnCalculateCachedValue
- ShouldUpdate
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);
}
}
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 LayoutSpec
s. 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.