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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
LINUX DO - 热门话题
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threat Research - Cisco Blogs
AI
AI
B
Blog RSS Feed
S
Schneier on Security
雷峰网
雷峰网
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
Cloudbric
Cloudbric
L
LINUX DO - 最新话题
罗磊的独立博客
有赞技术团队
有赞技术团队
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
The Hacker News
The Hacker News
博客园 - Franky
Attack and Defense Labs
Attack and Defense Labs
The Cloudflare Blog
Webroot Blog
Webroot Blog
Last Week in AI
Last Week in AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 叶小钗
美团技术团队
L
Lohrmann on Cybersecurity
T
The Blog of Author Tim Ferriss
The Last Watchdog
The Last Watchdog
T
Troy Hunt's Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
Know Your Adversary
Know Your Adversary
O
OpenAI News
博客园 - 【当耐特】
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cybersecurity and Infrastructure Security Agency CISA
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
PCI Perspectives
PCI Perspectives
H
Heimdal Security Blog
I
InfoQ
GbyAI
GbyAI
T
Threatpost
C
Cisco Blogs

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