ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android Jetpack Compose 可滚动列表 LazyColumn

2021-08-03 18:06:28  阅读:400  来源: 互联网

标签:Compose Jetpack LazyColumn content item add items


Android 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 的下拉刷新…

标签:Compose,Jetpack,LazyColumn,content,item,add,items
来源: https://blog.csdn.net/sinat_38184748/article/details/119355727

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有