Compose竖向列表LazyColumn

2023-10-19 03:04

本文主要是介绍Compose竖向列表LazyColumn,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

基础列表一

LazyColumn组件中用items加载数据,rememberLazyListState()结合rememberCoroutineScope()实现返回顶部。

/*** 基础列表一*/
@Composable
fun Items() {Box(modifier = Modifier.fillMaxSize()) {val context = LocalContext.currentval dataList = arrayListOf<Int>()for (index in 1..50) {dataList.add(index)}val listState = rememberLazyListState()LazyColumn(state = listState) {items(dataList) { data ->Text(text = "第${data}条数据",textAlign = TextAlign.Center,//属性设置先后顺序不一样运行效果会不一样modifier = Modifier.fillMaxWidth().padding(top = 1.dp).background(Color.White).clickable {Toast.makeText(context, "$data", Toast.LENGTH_SHORT).show()}.padding(10.dp))}}//返回顶部val coroutineScope = rememberCoroutineScope()Image(modifier = Modifier.padding(end = 10.dp, bottom = 10.dp).width(45.dp).height(45.dp).clip(CircleShape).align(Alignment.BottomEnd).background(Color.Blue).clickable {coroutineScope.launch {listState.animateScrollToItem(index = 0)}},painter = painterResource(id = R.drawable.top),contentDescription = "返回顶部图标")}
}

基础列表二

LazyColumn中通过itemsIndexed属性加载数据。

/*** 基础列表二*/
@Composable
fun ItemsIndexed() {val context = LocalContext.currentval stringList = arrayListOf<String>()for (index in 1..50) {stringList.add(index.toString())}LazyColumn {//stringList.toArray()方法可以通过List的toArray方法将List转为ArrayitemsIndexed(stringList) { index, data ->Text(text = "我的下标是:${index},我的数据为:$data",textAlign = TextAlign.Center,modifier = Modifier.fillMaxWidth().padding(top = 1.dp).background(Color.White).clickable {Toast.makeText(context, data, Toast.LENGTH_SHORT).show()})}}
}

多Type列表

 根据不同数据类型加载不同布局。

/*** 多Type列表*/
@Composable
fun AnyTypeList() {val charList = arrayListOf<Chat>()charList.apply {add(Chat("你好啊"))add(Chat("你在干啥呢"))add(Chat("想问你个事"))add(Chat("没干啥,还在写代码啊!", false))add(Chat("啥事啊大哥?", false))add(Chat("没事。。。"))add(Chat("好吧。。。", false))}LazyColumn {items(charList) { data ->if (data.isLeft) {Column(modifier = Modifier.fillMaxWidth().padding(start = 10.dp)) {//间隔设置Spacer(modifier = Modifier.height(10.dp))Row(verticalAlignment = Alignment.CenterVertically) {Image(modifier = Modifier.width(35.dp).height(35.dp)//裁剪圆形.clip(CircleShape),painter = painterResource(id = R.drawable.ic_launcher_background),contentDescription = "左头像")Spacer(modifier = Modifier.width(10.dp))Text(data.content,modifier = Modifier.wrapContentWidth().background(Color.Yellow).padding(10.dp),)}}} else {Column(modifier = Modifier.fillMaxWidth().padding(end = 10.dp),horizontalAlignment = Alignment.End) {Spacer(modifier = Modifier.height(10.dp))Row(verticalAlignment = Alignment.CenterVertically) {Text(data.content,modifier = Modifier.wrapContentWidth().background(Color.Green).padding(10.dp))Spacer(modifier = Modifier.width(10.dp))Image(modifier = Modifier.width(35.dp).height(35.dp).clip(CircleShape),painter = painterResource(id = R.drawable.ic_launcher_background),contentDescription = "右头像")}}}}}
}

数据类:

/*** created by cwj on 2023-10-16* Description:多类型列表类*/
data class Chat(val content: String, val isLeft: Boolean = true)

粘性标题列表

使用粘性标题stickyHeader组件。滑动列表,一级标题悬浮顶部,随着列表滑动顶部一级列表滑出被替换,顶部一直保持一项一级标题悬浮。

/*** 粘性标题列表*/
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun StickyHeaderTest() {val context = LocalContext.currentval letters = arrayListOf("类型一", "类型二", "类型三", "类型四", "类型五")val contactList = arrayListOf<Contact>()val nameList = arrayListOf<String>()for (index in 1..5) {nameList.add("子项$index")}for (index in letters.iterator()) {contactList.add(Contact(letters = index, nameList))}LazyColumn {contactList.forEach { (letter, nameList) ->stickyHeader {Text(text = letter,modifier = Modifier.background(Color.LightGray).padding(start = 10.dp).fillMaxWidth(),fontSize = 25.sp)}items(nameList) { contact ->Text(text = contact,modifier = Modifier.padding(10.dp).background(Color.White).fillMaxWidth().clickable {Toast.makeText(context, contact, Toast.LENGTH_SHORT).show()},textAlign = TextAlign.Center,fontSize = 25.sp)}}}
}

黏性标题和列表数据能对应起来的数据类:

/*** created by cwj on 2023-10-17* Description:黏性标题和列表数据能对应起来的数据类*/
data class Contact(val letters: String, val nameList: List<String>)

完整代码:

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.cwj.composedemo.ui.theme.ComposeDemoTheme
import kotlinx.coroutines.launchclass MainActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContent {ComposeDemoTheme {// A surface container using the 'background' color from the themeSurface(modifier = Modifier.fillMaxSize(),color = MaterialTheme.colorScheme.background) {Greeting()}}}}
}@Composable
fun Greeting() {
//    Items()
//    ItemsIndexed()
//    AnyTypeList()StickyHeaderTest()
}/*** 粘性标题列表*/
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun StickyHeaderTest() {val context = LocalContext.currentval letters = arrayListOf("类型一", "类型二", "类型三", "类型四", "类型五")val contactList = arrayListOf<Contact>()val nameList = arrayListOf<String>()for (index in 1..5) {nameList.add("子项$index")}for (index in letters.iterator()) {contactList.add(Contact(letters = index, nameList))}LazyColumn {contactList.forEach { (letter, nameList) ->stickyHeader {Text(text = letter,modifier = Modifier.background(Color.LightGray).padding(start = 10.dp).fillMaxWidth(),fontSize = 25.sp)}items(nameList) { contact ->Text(text = contact,modifier = Modifier.padding(10.dp).background(Color.White).fillMaxWidth().clickable {Toast.makeText(context, contact, Toast.LENGTH_SHORT).show()},textAlign = TextAlign.Center,fontSize = 25.sp)}}}
}/*** 多Type列表*/
@Composable
fun AnyTypeList() {val charList = arrayListOf<Chat>()charList.apply {add(Chat("你好啊"))add(Chat("你在干啥呢"))add(Chat("想问你个事"))add(Chat("没干啥,还在写代码啊!", false))add(Chat("啥事啊大哥?", false))add(Chat("没事。。。"))add(Chat("好吧。。。", false))}LazyColumn {items(charList) { data ->if (data.isLeft) {Column(modifier = Modifier.fillMaxWidth().padding(start = 10.dp)) {//间隔设置Spacer(modifier = Modifier.height(10.dp))Row(verticalAlignment = Alignment.CenterVertically) {Image(modifier = Modifier.width(35.dp).height(35.dp)//裁剪圆形.clip(CircleShape),painter = painterResource(id = R.drawable.ic_launcher_background),contentDescription = "左头像")Spacer(modifier = Modifier.width(10.dp))Text(data.content,modifier = Modifier.wrapContentWidth().background(Color.Yellow).padding(10.dp),)}}} else {Column(modifier = Modifier.fillMaxWidth().padding(end = 10.dp),horizontalAlignment = Alignment.End) {Spacer(modifier = Modifier.height(10.dp))Row(verticalAlignment = Alignment.CenterVertically) {Text(data.content,modifier = Modifier.wrapContentWidth().background(Color.Green).padding(10.dp))Spacer(modifier = Modifier.width(10.dp))Image(modifier = Modifier.width(35.dp).height(35.dp).clip(CircleShape),painter = painterResource(id = R.drawable.ic_launcher_background),contentDescription = "右头像")}}}}}
}/*** 基础列表二*/
@Composable
fun ItemsIndexed() {val context = LocalContext.currentval stringList = arrayListOf<String>()for (index in 1..50) {stringList.add(index.toString())}LazyColumn {//stringList.toArray()方法可以通过List的toArray方法将List转为ArrayitemsIndexed(stringList) { index, data ->Text(text = "我的下标是:${index},我的数据为:$data",textAlign = TextAlign.Center,modifier = Modifier.fillMaxWidth().padding(top = 1.dp).background(Color.White).clickable {Toast.makeText(context, data, Toast.LENGTH_SHORT).show()})}}
}/*** 基础列表一*/
@Composable
fun Items() {Box(modifier = Modifier.fillMaxSize()) {val context = LocalContext.currentval dataList = arrayListOf<Int>()for (index in 1..50) {dataList.add(index)}val listState = rememberLazyListState()LazyColumn(state = listState) {items(dataList) { data ->Text(text = "第${data}条数据",textAlign = TextAlign.Center,//属性设置先后顺序不一样运行效果会不一样modifier = Modifier.fillMaxWidth().padding(top = 1.dp).background(Color.White).clickable {Toast.makeText(context, "$data", Toast.LENGTH_SHORT).show()}.padding(10.dp))}}//返回顶部val coroutineScope = rememberCoroutineScope()Image(modifier = Modifier.padding(end = 10.dp, bottom = 10.dp).width(45.dp).height(45.dp).clip(CircleShape).align(Alignment.BottomEnd).background(Color.Blue).clickable {coroutineScope.launch {listState.animateScrollToItem(index = 0)}},painter = painterResource(id = R.drawable.top),contentDescription = "返回顶部图标")}
}@Preview(showBackground = true, showSystemUi = true)
@Composable
fun GreetingPreview() {ComposeDemoTheme {Greeting()}
}

这篇关于Compose竖向列表LazyColumn的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/236866

相关文章

通过Docker Compose部署MySQL的详细教程

《通过DockerCompose部署MySQL的详细教程》DockerCompose作为Docker官方的容器编排工具,为MySQL数据库部署带来了显著优势,下面小编就来为大家详细介绍一... 目录一、docker Compose 部署 mysql 的优势二、环境准备与基础配置2.1 项目目录结构2.2 基

Python中DataFrame转列表的最全指南

《Python中DataFrame转列表的最全指南》在Python数据分析中,Pandas的DataFrame是最常用的数据结构之一,本文将为你详解5种主流DataFrame转换为列表的方法,大家可以... 目录引言一、基础转换方法解析1. tolist()直接转换法2. values.tolist()矩阵

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

python展开嵌套列表的多种方法

《python展开嵌套列表的多种方法》本文主要介绍了python展开嵌套列表的多种方法,包括for循环、列表推导式和sum函数三种方法,具有一定的参考价值,感兴趣的可以了解一下... 目录一、嵌套列表格式二、嵌套列表展开方法(一)for循环(1)for循环+append()(2)for循环+pyPhWiFd

Python容器类型之列表/字典/元组/集合方式

《Python容器类型之列表/字典/元组/集合方式》:本文主要介绍Python容器类型之列表/字典/元组/集合方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 列表(List) - 有序可变序列1.1 基本特性1.2 核心操作1.3 应用场景2. 字典(D

Java中数组转换为列表的两种实现方式(超简单)

《Java中数组转换为列表的两种实现方式(超简单)》本文介绍了在Java中将数组转换为列表的两种常见方法使用Arrays.asList和Java8的StreamAPI,Arrays.asList方法简... 目录1. 使用Java Collections框架(Arrays.asList)1.1 示例代码1.

python中列表list切分的实现

《python中列表list切分的实现》列表是Python中最常用的数据结构之一,经常需要对列表进行切分操作,本文主要介绍了python中列表list切分的实现,文中通过示例代码介绍的非常详细,对大家... 目录一、列表切片的基本用法1.1 基本切片操作1.2 切片的负索引1.3 切片的省略二、列表切分的高

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

Redis存储的列表分页和检索的实现方法

《Redis存储的列表分页和检索的实现方法》在Redis中,列表(List)是一种有序的数据结构,通常用于存储一系列元素,由于列表是有序的,可以通过索引来访问元素,因此可以很方便地实现分页和检索功能,... 目录一、Redis 列表的基本操作二、分页实现三、检索实现3.1 方法 1:客户端过滤3.2 方法

Python实现将实体类列表数据导出到Excel文件

《Python实现将实体类列表数据导出到Excel文件》在数据处理和报告生成中,将实体类的列表数据导出到Excel文件是一项常见任务,Python提供了多种库来实现这一目标,下面就来跟随小编一起学习一... 目录一、环境准备二、定义实体类三、创建实体类列表四、将实体类列表转换为DataFrame五、导出Da