Litho
  • Introduction
  • 简介
    • Litho 是什么
    • 动机
    • 使用
  • 快速开始
    • 新手入门
    • 教程
    • 编写 Component
    • 使用 Component
  • 参考
    • Layout Specs
    • Mount Specs
    • Props
    • State
    • Layout
    • Error Boundaries
    • Borders
    • RecyclerCollectionComponent
    • 变换动画
    • 术语
  • 事件处理
    • 总览
    • 触摸处理
    • 可见性处理
  • Sections
    • Sections 是什么
    • Sections 教程
    • GroupSection Specs
    • DiffSection Specs
    • Sections Building Blocks
    • 与UI交互
    • Sections 单元测试
    • Sections 与 Views
    • Services
    • Sections 架构
    • Working Ranges
  • 兼容性
    • Styles
    • Accessbility
    • RTL
  • 测试
    • 总览
    • 单元测试基础
    • 子组件测试
    • 匹配 Props
    • 测试 InjectProps
    • 测试事件处理
    • Espresso
    • 在Android Studio上运行测试
  • 高级指南
    • Recycler
    • 自定义布局
    • TreeProps
    • 增量安装
    • 创建 ComponentTree
  • 架构
    • 代码生成
    • 异步布局
    • 增量安装
    • 铺平 View 结构
    • 回收
  • 附加资源
    • 最佳实践
    • FAQ
  • 工具
    • 调试
    • 开发者选项
  • 更多参与
    • 如何参与
    • 社区展示
    • 资源库结构
Powered by GitBook
On this page
  • 将Litho引入到项目中
  • 在项目中添加Sections的依赖
  • 使用快照版本
  • 安装测试
  1. 快速开始

新手入门

将Litho引入到项目中

我们把Litho发布到了Bintray的JCenter仓库中。要将Litho引入到Android项目中,确保在build.gradle文件中添加对JCenter的引用。

repositories {
  jcenter()
}

之后添加如下依赖:

dependencies {
  // ...
  // Litho
  implementation 'com.facebook.litho:litho-core:0.14.0'
  implementation 'com.facebook.litho:litho-widget:0.14.0'
  compileOnly 'com.facebook.litho:litho-annotations:0.14.0'

  annotationProcessor 'com.facebook.litho:litho-processor:0.14.0'

  // SoLoader
  implementation 'com.facebook.soloader:soloader:0.2.0'

  // For integration with Fresco
  implementation 'com.facebook.litho:litho-fresco:0.14.0'

  // For testing
  testImplementation 'com.facebook.litho:litho-testing:0.14.0'
}

在项目中添加Sections的依赖

Litho自带Sections可选库用于声明式构建列表,引入Sections需要额外把以下依赖添加到build.gradle文件中。

dependencies {

  // Sections
  implementation 'com.facebook.litho:litho-sections-core:0.14.0'
  implementation 'com.facebook.litho:litho-sections-widget:0.14.0'
  compileOnly 'com.facebook.litho:litho-sections-annotations:0.14.0'

  annotationProcessor 'com.facebook.litho:litho-sections-processor:0.14.0'
}

使用快照版本

IMPORTANT: 快照版本可能会摧毁或者把 家的房子烧掉(ps:对项目造成较大影响),快照版本未经签名并且由我们的CI系统自动发布,确保仅将快照版本用于测试目的。

首先,将Sonatype快照库添加到Gradle配置中:

repositories {
  maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}

之后就可以访问到我们发布的所有快照版本:

dependencies {
  // ...
  // Litho
  implementation 'com.facebook.litho:litho-core:0.14.1-SNAPSHOT'
  implementation 'com.facebook.litho:litho-widget:0.14.1-SNAPSHOT'
  compileOnly 'com.facebook.litho:litho-annotations:0.14.1-SNAPSHOT'

  annotationProcessor 'com.facebook.litho:litho-processor:0.14.1-SNAPSHOT'

  // SoLoader
  implementation 'com.facebook.soloader:soloader:0.2.0'

  // For integration with Fresco
  implementation 'com.facebook.litho:litho-fresco:0.14.1-SNAPSHOT'

  // For testing
  testImplementation 'com.facebook.litho:litho-testing:0.14.1-SNAPSHOT'
}

安装测试

在Activity中添加一个用Litho创建的View来测试安装是否成功。

[MyApplication.java]

public class MyApplication extends Application {

  @Override
  public void onCreate() {
    super.onCreate();

    SoLoader.init(this, false);
  }
}

之后添加Litho预定义的Text组件到Activity中显示 “Hello World!”。

[MyActivity.java]

import com.facebook.litho.ComponentContext;
import com.facebook.litho.LithoView;

public class MyActivity extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final ComponentContext c = new ComponentContext(this);

    final LithoView lithoView = LithoView.create(
        this /* context */,
        Text.create(c)
            .text("Hello, World!")
            .textSizeDip(50)
            .build());

    setContentView(lithoView);
  }
}

现在运行APP,可以在屏幕上看到 “Hello World!”。

Previous快速开始Next教程

Last updated 7 years ago

首先,初始化SoLoader.Litho依赖帮助加载由底层的布局引擎提供的本地库。应用的Application里适合进行初始化:

SoLoader
Yoga