惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Securelist
博客园 - Franky
B
Blog RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
量子位
Hugging Face - Blog
Hugging Face - Blog
有赞技术团队
有赞技术团队
V
V2EX
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
L
LangChain Blog
大猫的无限游戏
大猫的无限游戏
雷峰网
雷峰网
G
Google Developers Blog
B
Blog
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
小众软件
小众软件
博客园 - 【当耐特】
H
Help Net Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
博客园 - 叶小钗
Jina AI
Jina AI
Cloudbric
Cloudbric
N
Netflix TechBlog - Medium
Hacker News - Newest:
Hacker News - Newest: "LLM"
P
Proofpoint News Feed
L
Lohrmann on Cybersecurity
I
Intezer
IT之家
IT之家
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hackread – Cybersecurity News, Data Breaches, AI and More
WordPress大学
WordPress大学
W
WeLiveSecurity
G
GRAHAM CLULEY
J
Java Code Geeks
H
Heimdal Security Blog
Cyberwarzone
Cyberwarzone
MyScale Blog
MyScale Blog
Latest news
Latest news
Schneier on Security
Schneier on Security
H
Hacker News: Front Page
Martin Fowler
Martin Fowler
V
Visual Studio Blog
Webroot Blog
Webroot Blog
P
Palo Alto Networks Blog
T
Tor Project blog

LengYue's Blog

在 Android 中实现 iOS 液态玻璃效果(二:折射与渗透效果) | LengYue's Blog 在 Android 中实现 iOS 液态玻璃效果 | LengYue's Blog Dart 单线程模型的本质 | LengYue's Blog 使用 Rust 开发 Android 底层库,并简化 Java 与 Rust 相互操作 | LengYue's Blog 将 Rust 编译为可在 Android 上使用的二进制文件 | LengYue's Blog 使用 GoMobile 创建 Android、iOS 跨平台 WebSocket Library | LengYue's Blog Flutter error not found dart html | LengYue's Blog AndroidStudio新版本Logcat | LengYue's Blog Typecho to Hugo | LengYue's Blog GetX Router 设置返回值 | LengYue's Blog Flutter 基础 | Dart 语法 mixin | LengYue's Blog 不使用第三方软件实现Obsidian多平台实时同步 | LengYue's Blog Android 使用字节码 hook 修复第三发 bug | LengYue's Blog 在 Jetpack Compose 中使用输入框(TextField )遇到的一些问题 | LengYue's Blog Android-使用@AutoService实现spi | LengYue's Blog RecyclerView使用优化的ListAdapter | LengYue's Blog ⾃定义布局流程 | LengYue's Blog ViewModel之自定义构造函数 | LengYue's Blog Android Jetpack Compose 超快速上手指南 | LengYue's Blog android 11 [api 30] 适配指南 | LengYue's Blog Android 声明式 UI 框架 Litho 初探 —— Layout | LengYue's Blog Android声明式UI框架 Litho 初探 —— MountSpec的使用 | LengYue's Blog Android声明式UI框架 Litho 初探——基础使用 | LengYue's Blog Android声明式UI框架 Litho 初探 ——两种数据类型 | LengYue's Blog
Android 声明式 UI 框架 Litho 初探 —— Sections API | LengYue's Blog
LengYue · 2020-09-09 · via LengYue's Blog

在最开始入门介绍中,我们曾经用 SingleComponentSection 完成了一个简单的列表,当时的做法是使用 for 构造出了多个子 Component。其实在 Litho 中提供了一个性能更好的方式,专门处理这种数据(这种数据其实就是类似于 Android 中的 adapter 与其绑定的数据)。 Litho 中专门处理这种模板与列表支持的组件叫做

DataDiffSection 的使用

DataDiffSection。下面用 DataDiffSection 我们重构一下之前写的MainListViewSpec

  • 首先生成我们的数据:
 val data = arrayListOf<Int>()
        for (i in 0 until 32) {
            data.add(i)
        }
  • MainListViewSpec中增加一个创建ListItemView 组件的方法:
    @OnEvent(RenderEvent::class)
    fun onRender(c: SectionContext, @FromEvent model: Int): RenderInfo {
        return ComponentRenderInfo.create().component(
            ListItemView.create(c)
                .color(if (model % 2 == 0) Color.WHITE else Color.LTGRAY)
                .title(if (model % 2 == 0) "hello word" else model.toString())
                .build()
        ).build()
    }
  • 接下来改造一下 onCreateChildren 方法:
    @OnCreateChildren
    fun createChildren(c: SectionContext, @Prop listData: ArrayList<Int>):Children {
        return Children.create()
            .child(
                DataDiffSection.create<Int>(c)
                    .data(listData)
                    .renderEventHandler(MainListView.onRender(c))
            )
            .build()
    }
  • 最后运行一下:
···
 val recycleView = RecyclerCollectionComponent.create(c).disablePTR(false)
            .section(MainListView.create(SectionContext(this)).listData(data)).build()
        setContentView(LithoView.create(c, recycleView))
···

可能大部分到这里都有点蒙,DataDiffSection 到底是从哪里来的呢? DataDiffSection 有点类似于 Android 的 DiffUtil 它是一个内置的一个事件:

  1. 每当一个 Item 被渲染的时候,DataDiffSection 会产生一个 RenderEvent。
  2. 创建 DataDiffSection 的时候,我们要传入自己的 renderEventHandler,就是上面代码中的MainListView.onRender(c)。 可以看到效果跟之前的没有区别:

嵌套一个横向滚动的列表

在最开始我们是使用 SingleComponentSection 构建列表的。这里如果需要嵌套一个横向滚动的列表,同样也可以用 SingleComponentSection 来完成:

val config =  ListRecyclerConfiguration.create()
            .orientation(LinearLayoutManager.HORIZONTAL)
            .reverseLayout(false)
            .snapMode(SnapUtil.SNAP_TO_CENTER)
            .build()
 RecyclerCollectionComponent.create(c)
    .disablePTR(true)
    .recyclerConfiguration(config)
    .section(DataDiffSection.create<Int>(c)
        .data(listData)
        .renderEventHandler(SectionItem.onRender(c)))
    .canMeasureRecycler(true)

![Video_20200909_112127_733.gif][2] [2]: https://raw.githubusercontent.com/appdev/gallery/main/img/3c3c3037b9b54ce4a9f772778fcf29f1%7Etplv-k3u1fbpfcp-zoom-in-crop-mark_1512_0_0_0.webp