本文主要是介绍Android Jetpack Compose 可滚动列表 LazyColumn,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android Jetpack Compose 可滚动列表 LazyColumn
- Jetpack Compose
- 前言
- LazyColumn
- 使用方法
- 完事
Jetpack Compose
更多与Jetpack Compose相关的文章:
Android Jetpack Compose 沉浸式/透明状态栏 ProvideWindowInsets SystemUiController
前言
这是一个比较简单的知识点,但是在当前时间2021/08/03,在搜索引擎上还没能快速搜索到相关信息,所以贡献一下自己的了解。
LazyColumn
这个库来自androidx.compose.foundation.lazy
/*** The vertically scrolling list that only composes and lays out the currently visible items. The content block defines a DSL which allows you to emit items of different types. For example you can use LazyListScope.item to add a single item and LazyListScope.items to add a list of items.* Params:* modifier - the modifier to apply to this layout.* state - the state object to be used to control or observe the list's state.* contentPadding - a padding around the whole content. This will add padding for the. content after it has been clipped, which is not possible via modifier param. You can use it to add a padding before the first item or after the last one. If you want to add a spacing between each item use verticalArrangement.* reverseLayout - reverse the direction of scrolling and layout, when true items will be composed from the bottom to the top and LazyListState.firstVisibleItemIndex == 0 will mean we scrolled to the bottom.* verticalArrangement - The vertical arrangement of the layout's children. This allows to add a spacing between items and specify the arrangement of the items when we have not enough of them to fill the whole minimum size.* horizontalAlignment - the horizontal alignment applied to the items.* flingBehavior - logic describing fling behavior.* content - a block which describes the content. Inside this block you can use methods like LazyListScope.item to add a single item or LazyListScope.items to add a list of items.* Samples:* androidx.compose.foundation.samples.LazyColumnSample// Unresolved
*/
@Composable
fun LazyColumn(modifier: Modifier = Modifier,state: LazyListState = rememberLazyListState(),contentPadding: PaddingValues = PaddingValues(0.dp),reverseLayout: Boolean = false,verticalArrangement: Arrangement.Vertical =if (!reverseLayout) Arrangement.Top else Arrangement.Bottom,horizontalAlignment: Alignment.Horizontal = Alignment.Start,flingBehavior: FlingBehavior = ScrollableDefaults.flingBehavior(),content: LazyListScope.() -> Unit
) {LazyList(stateOfItemsProvider = rememberStateOfItemsProvider(content),modifier = modifier,state = state,contentPadding = contentPadding,flingBehavior = flingBehavior,horizontalAlignment = horizontalAlignment,verticalArrangement = verticalArrangement,isVertical = true,reverseLayout = reverseLayout)
}
从注解信息就能了解到这是一个支持纵向滚动的列表
使用方法
LazyColumn(// 列表反向显示//reverseLayout = true
) {// 显示列表items(list.size) { index ->val info = list[index]// 列表单项的UI}// 显示单个的UI,可以用作多个列表的分割,或者头部、底部之类的item {Text("这是底部", Modifier.padding(16.dp))}
}
同理,横向滚动就可以选择LazyRow
完事
下一篇应该会说 Android Jetpack Compose 的下拉刷新…
这篇关于Android Jetpack Compose 可滚动列表 LazyColumn的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!