什么是RepackerView或ListView的Jetpack Compose等效项? [英] What is the Jetpack Compose equivalent of RecyclerView or ListView?

查看:492
本文介绍了什么是RepackerView或ListView的Jetpack Compose等效项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Jetpack Compose中,如何在仅布局可见项目的同时显示大量数据,而不是在初始布局遍历中对每个项目进行布局和布局?这类似于View工具箱中的RecyclerViewListView.

In Jetpack Compose, how can I display a large list of data while laying out only the visible items, instead of composing and laying out every item on the initial layout pass? This would be similar to RecyclerView and ListView in the View toolkit.

一个人可以使用for循环将所有组件放置在VerticalScroller中的Column内,但这会导致丢帧和大量项目上的性能下降.

One could use a for loop to place all of the components inside of a Column in a VerticalScroller, but this would result in dropped frames and poor performance on larger numbers of items.

注意:这旨在作为规范的自我回答问题来抢先/处理类似问题

Note: this is intended as a canonical self-answered question to pre-empt/handle similar questions

推荐答案

Jetpack Compose中与RecyclerViewListView等效的组件是

The equivalent component to RecyclerView or ListView in Jetpack Compose is LazyColumnFor for a vertical list and LazyRowFor for a horizontal list. These compose and lay out only the currently visible items.

通过将数据格式化为列表并通过@Composable回调传递数据来使用它,该回调为列表中的给定项目发出UI.例如:

You use it by formatting your data as a list and passing it with a @Composable callback that emits the UI for a given item in the list. For example:

val myData = listOf("Hello,", "world!")
LazyColumnFor(myData) { item ->
    Text(text = item)
}

val myData = listOf("Hello,", "world!")
LazyRowFor(myData) { item ->
    Text(text = item)
}

还有索引变体,除了项目本身外,它们还提供集合中的索引:

There are also indexed variants, which provide the index in the collection in addition to the item itself:

val myData = listOf("Hello,", "world!")
LazyColumnForIndexed(myData) { index, item ->
    Text(text = "Item #$index is $item")
}

val myData = listOf("Hello,", "world!")
LazyRowForIndexed(myData) { index, item ->
    Text(text = "Item #$index is $item")
}

在以前的版本中,这些API被称为AdapterListLazyColumnItems/LazyRowItems.

These APIs were, in previous releases, known as AdapterList and LazyColumnItems/LazyRowItems.

这篇关于什么是RepackerView或ListView的Jetpack Compose等效项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆