Text是在com.facebook.litho.widget包中定义的一个核心组件。包含一些像上面显示的属性,例如 text 和 textSize。这些属性叫做props,是受React中术语的启发而来的。随后将会学习如何编写自己的组件,但是值得注意的是,这里的Text 类是有一个叫做TextSpec的类生成的。这个生成的组件类提供一个建造者模式的API,通过方法为组件设置属性值。
SingleComponentSection是在com.facebook.litho.sections.widget包中定义的一个核心Section,用于渲染单一的组件。ListSectionSpec描述了一个包含有32个子Section的section,每个Section负责渲染一个ListItem.可以用这个Section结合RecyclerCollectionComponent来渲染列表。RecyclerCollectionComponent 接受一个Section作为属性,会渲染一个RecylerView,此RecylerView包含此Section输出的任意UI。 它也会管理来自Section的更新和变化例如数据刷新和尾部数据获取。此处我们不使用数据获取功能,所以在这个教程中关闭掉PTR(pull to refresh)功能.在Activity,把Component的定义改成下面的代码:
@LayoutSpec
public class ListItemSpec {
@OnCreateLayout
static Component onCreateLayout(ComponentContext c) {
return Column.create(c)
.paddingDip(ALL, 16)
.backgroundColor(Color.WHITE)
.child(
Text.create(c)
.text("Hello world")
.textSizeSp(40))
.child(
Text.create(c)
.text("Litho tutorial")
.textSizeSp(20))
.build();
}
}
final Component component = ListItem.create(context).build();
@GroupSectionSpec
public class ListSectionSpec {
@OnCreateChildren
static Children onCreateChildren(final SectionContext c) {
Children.Builder builder = Children.create();
for (int i = 0; i < 32; i++) {
builder.child(
SingleComponentSection.create(c)
.key(String.valueOf(i))
.component(ListItem.create(c).build()));
}
return builder.build();
}
}
final Component component =
RecyclerCollectionComponent.create(context)
.disablePTR(true)
.section(ListSection.create(new SectionContext(context)).build())
.build();