首页 > 其他分享 >Android开发 Jetpack compose LazyColumn 与 LazyRow、LazyVerticalGrid、LazyHorizontalGrid、LazyVerticalStagg

Android开发 Jetpack compose LazyColumn 与 LazyRow、LazyVerticalGrid、LazyHorizontalGrid、LazyVerticalStagg

时间:2023-08-08 14:56:52浏览次数:42  
标签:LazyHorizontalGrid compose listData Jetpack mipmap fruit ic Modifier dp

前言

  此篇博客讲解LazyColumn 与 LazyRow、LazyVerticalGrid、LazyHorizontalGrid,在compose里LazyColumn与LazyRow与是用来延迟加载数据的,它对标原来xml里的ListView与RecyclerView。

LazyColumn 纵向列表

效果图

代码

@Composable
fun APage() {
    val listData = remember {
        //mutableStateListOf 这个很关键,需要动态新增数据一定需要使用mutableStateListOf
        mutableStateListOf(
            "苹果" to R.mipmap.ic_fruit_apple,
            "香蕉" to R.mipmap.ic_fruit_banana,
            "牛油果" to R.mipmap.ic_fruit_avocado,
            "蓝莓" to R.mipmap.ic_fruit_blueberry,
            "椰子" to R.mipmap.ic_fruit_coconut,
            "葡萄" to R.mipmap.ic_fruit_grape,
            "哈密瓜" to R.mipmap.ic_fruit_hami_melon,
            "猕猴桃" to R.mipmap.ic_fruit_kiwifruit,
            "柠檬" to R.mipmap.ic_fruit_lemon,
            "荔枝" to R.mipmap.ic_fruit_litchi,
            "芒果" to R.mipmap.ic_fruit_mango,
        )
    }
    Column {
        Button(modifier = Modifier.fillMaxWidth(), onClick = { listData.add("山竹" to R.mipmap.ic_fruit_mangosteen) }) {
            Text(text = "新增数据")
        }
        //LazyColumn
        LazyColumn(
            Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.Center
        ) {
            //这个items很关键,LazyColumn一定需要使用这个
            items(listData.size){
                Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(20.dp)) {
                    Image(painter = painterResource(id = listData[it].second), contentDescription = null, modifier = Modifier
                        .width(100.dp)
                        .height(100.dp))
                    Text(text = listData[it].first, fontSize = 25.sp, modifier = Modifier.padding(start = 80.dp))
                }
            }
        }
    }
}

LazyRow 横向列表

效果图

代码

@Composable
fun APage() {
    val listData = remember {
        //mutableStateListOf 这个很关键,需要动态新增数据一定需要使用mutableStateListOf
        mutableStateListOf(
            "苹果" to R.mipmap.ic_fruit_apple,
            "香蕉" to R.mipmap.ic_fruit_banana,
            "牛油果" to R.mipmap.ic_fruit_avocado,
            "蓝莓" to R.mipmap.ic_fruit_blueberry,
            "椰子" to R.mipmap.ic_fruit_coconut,
            "葡萄" to R.mipmap.ic_fruit_grape,
            "哈密瓜" to R.mipmap.ic_fruit_hami_melon,
            "猕猴桃" to R.mipmap.ic_fruit_kiwifruit,
            "柠檬" to R.mipmap.ic_fruit_lemon,
            "荔枝" to R.mipmap.ic_fruit_litchi,
            "芒果" to R.mipmap.ic_fruit_mango,
        )
    }
    Column {
        Button(modifier = Modifier.fillMaxWidth(), onClick = { listData.add("山竹" to R.mipmap.ic_fruit_mangosteen) }) {
            Text(text = "新增数据")
        }
        //LazyRow
        LazyRow(
            Modifier.fillMaxSize(),
        ) {
            //这个items很关键
            items(listData.size){
                Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.padding(20.dp)) {
                    Image(painter = painterResource(id = listData[it].second), contentDescription = null, modifier = Modifier
                        .width(100.dp)
                        .height(100.dp))
                    Text(text = listData[it].first, fontSize = 25.sp)
                }
            }
        }
    }
}

LazyVerticalGrid 纵向宫格列表

效果图

代码

@Composable
fun APage() {
    val listData = remember {
        //mutableStateListOf 这个很关键,需要动态新增数据一定需要使用mutableStateListOf
        mutableStateListOf(
            "苹果" to R.mipmap.ic_fruit_apple,
            "香蕉" to R.mipmap.ic_fruit_banana,
            "牛油果" to R.mipmap.ic_fruit_avocado,
            "蓝莓" to R.mipmap.ic_fruit_blueberry,
            "椰子" to R.mipmap.ic_fruit_coconut,
            "葡萄" to R.mipmap.ic_fruit_grape,
            "哈密瓜" to R.mipmap.ic_fruit_hami_melon,
            "猕猴桃" to R.mipmap.ic_fruit_kiwifruit,
            "柠檬" to R.mipmap.ic_fruit_lemon,
            "荔枝" to R.mipmap.ic_fruit_litchi,
            "芒果" to R.mipmap.ic_fruit_mango,
            "橘子" to R.mipmap.ic_fruit_orange,
            "梨子" to R.mipmap.ic_fruit_pear,
        )
    }
    Column {
        Button(modifier = Modifier.fillMaxWidth(), onClick = { listData.add("山竹" to R.mipmap.ic_fruit_mangosteen) }) {
            Text(text = "新增数据")
        }
        //LazyVerticalGrid
        //columns 设置显示几列
        LazyVerticalGrid(columns = GridCells.Fixed(3)) {
            items(listData.size){
                Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.padding(20.dp)) {
                    Image(painter = painterResource(id = listData[it].second), contentDescription = null, modifier = Modifier
                        .width(90.dp)
                        .height(90.dp))
                    Text(text = listData[it].first, fontSize = 18.sp)
                }
            }
        }
    }
}

LazyHorizontalGrid横向宫格列表

效果图

代码

@Composable
fun APage() {
    val listData = remember {
        //mutableStateListOf 这个很关键,需要动态新增数据一定需要使用mutableStateListOf
        mutableStateListOf(
            "苹果" to R.mipmap.ic_fruit_apple,
            "香蕉" to R.mipmap.ic_fruit_banana,
            "牛油果" to R.mipmap.ic_fruit_avocado,
            "蓝莓" to R.mipmap.ic_fruit_blueberry,
            "椰子" to R.mipmap.ic_fruit_coconut,
            "葡萄" to R.mipmap.ic_fruit_grape,
            "哈密瓜" to R.mipmap.ic_fruit_hami_melon,
            "猕猴桃" to R.mipmap.ic_fruit_kiwifruit,
            "柠檬" to R.mipmap.ic_fruit_lemon,
            "荔枝" to R.mipmap.ic_fruit_litchi,
            "芒果" to R.mipmap.ic_fruit_mango,
            "橘子" to R.mipmap.ic_fruit_orange,
            "梨子" to R.mipmap.ic_fruit_pear,
        )
    }
    Column {
        Button(modifier = Modifier.fillMaxWidth(), onClick = { listData.add("山竹" to R.mipmap.ic_fruit_mangosteen) }) {
            Text(text = "新增数据")
        }
        //LazyHorizontalGrid
        //rows 设置显示几行
        LazyHorizontalGrid(rows = GridCells.Fixed(4)) {
            items(listData.size){
                Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.padding(20.dp)) {
                    Image(painter = painterResource(id = listData[it].second), contentDescription = null, modifier = Modifier
                        .width(90.dp)
                        .height(90.dp))
                    Text(text = listData[it].first, fontSize = 18.sp)
                }
            }
        }
    }
}

LazyVerticalStaggeredGrid 纵向瀑布列表

请注意!LazyVerticalStaggeredGrid为实验性API,并且Compose版本需要1.4.0以上。 这个其实跟我另一篇博客介绍的东西差不多 Android开发 Jetpack Compose FlowColumn与FlowRow瀑布流布局

效果图

代码

//因为LazyVerticalStaggeredGrid是实验性API,所以需要这个注解
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun APage() {
    val listData = remember {
        mutableStateListOf(
            "苹果" to R.mipmap.ic_fruit_apple,
            "香蕉" to R.mipmap.ic_fruit_banana,
            "牛油果" to R.mipmap.ic_fruit_avocado,
            "蓝莓" to R.mipmap.ic_fruit_blueberry,
            "椰子" to R.mipmap.ic_fruit_coconut,
            "葡萄" to R.mipmap.ic_fruit_grape,
            "哈密瓜" to R.mipmap.ic_fruit_hami_melon,
            "猕猴桃" to R.mipmap.ic_fruit_kiwifruit,
            "柠檬" to R.mipmap.ic_fruit_lemon,
            "荔枝" to R.mipmap.ic_fruit_litchi,
            "芒果" to R.mipmap.ic_fruit_mango,
        )
    }
    Column {
        //LazyVerticalStaggeredGrid
        //StaggeredGridCells设置显示几列
        LazyVerticalStaggeredGrid(columns = StaggeredGridCells.Fixed(4)) {
            items(listData.size){
                //这边设置一个item的随机高度,产生随机高度的瀑布效果
                val height = (80..200).random()
                Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.padding(10.dp).background(
                    Color.Gray)) {
                    Image(painter = painterResource(id = listData[it].second), contentDescription = null, modifier = Modifier
                        .width(90.dp)
                        .height(height.dp))
                    Text(text = listData[it].first, fontSize = 18.sp)
                }
            }
        }
    }
}

LazyHorizontalStaggeredGrid 横向瀑布列表

效果图

代码

//因为LazyHorizontalStaggeredGrid是实验性API,所以需要这个注解
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun APage() {
    val listData = remember {
        mutableStateListOf(
            "苹果" to R.mipmap.ic_fruit_apple,
            "香蕉" to R.mipmap.ic_fruit_banana,
            "牛油果" to R.mipmap.ic_fruit_avocado,
            "蓝莓" to R.mipmap.ic_fruit_blueberry,
            "椰子" to R.mipmap.ic_fruit_coconut,
            "葡萄" to R.mipmap.ic_fruit_grape,
            "哈密瓜" to R.mipmap.ic_fruit_hami_melon,
            "猕猴桃" to R.mipmap.ic_fruit_kiwifruit,
            "柠檬" to R.mipmap.ic_fruit_lemon,
            "荔枝" to R.mipmap.ic_fruit_litchi,
            "芒果" to R.mipmap.ic_fruit_mango,
        )
    }
    Column {
        //LazyHorizontalStaggeredGrid
        //StaggeredGridCells设置显示几行
        LazyHorizontalStaggeredGrid(rows = StaggeredGridCells.Fixed(4)) {
            items(listData.size){
                //这边设置一个item的随机宽度,产生随机宽度的瀑布效果
                val width = (80..200).random()
                Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.padding(10.dp).background(
                    Color.Gray)) {
                    Image(painter = painterResource(id = listData[it].second), contentDescription = null, modifier = Modifier
                        .width(width.dp)
                        .height(90.dp))
                    Text(text = listData[it].first, fontSize = 18.sp)
                }
            }
        }
    }
}

列表的滚动监听与设置滚动位置

这里使用LazyColumn举例,因为其他形式的列表基本差不多。如果你不太了解下面代码中的rememberCoroutineScope,请参考博客 Android开发 Jetpack_Compose_6 附带效应  这属于附带效应中的知识。 如果你疑问为什么要使用rememberCoroutineScope?这是因为onClick不属于协程域和compose域,而rememberLazyListState需要协程域内才能调用。

效果图

代码

@Composable
fun APage() {
    val listData = remember {
        mutableStateListOf(
            "苹果" to R.mipmap.ic_fruit_apple,
            "香蕉" to R.mipmap.ic_fruit_banana,
            "牛油果" to R.mipmap.ic_fruit_avocado,
            "蓝莓" to R.mipmap.ic_fruit_blueberry,
            "椰子" to R.mipmap.ic_fruit_coconut,
            "葡萄" to R.mipmap.ic_fruit_grape,
            "哈密瓜" to R.mipmap.ic_fruit_hami_melon,
            "猕猴桃" to R.mipmap.ic_fruit_kiwifruit,
            "柠檬" to R.mipmap.ic_fruit_lemon,
            "荔枝" to R.mipmap.ic_fruit_litchi,
            "芒果" to R.mipmap.ic_fruit_mango,
        )
    }
    //协程范围
    val coroutineScope = rememberCoroutineScope()
    //关键代码!滚动状态监听
    val listState = rememberLazyListState()
    Column {
        DescribeText(text = "当前位置 = ${listState.firstVisibleItemIndex}")
        DescribeText(text = "滚动偏移量 = ${listState.firstVisibleItemScrollOffset}")
        DescribeText(text = "是否正在滚动 = ${listState.isScrollInProgress}")
        DescribeText(text = "可以向前滚动(是否可以向下滚) = ${listState.canScrollForward}")
        DescribeText(text = "可以向后滚动(是否可以向上滚) = ${listState.canScrollBackward}")
        Button(modifier = Modifier.fillMaxWidth(), onClick = {
            coroutineScope.launch {
                listState.scrollToItem(0)//关键代码!点击回到最上面
            }
        }) {
            Text(text = "回到最上面")
        }
        LazyColumn(
            state = listState,
            verticalArrangement = Arrangement.Center,
            modifier = Modifier.fillMaxSize(),
        ) {
            items(listData.size) {
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier.padding(20.dp)
                ) {
                    Image(
                        painter = painterResource(id = listData[it].second),
                        contentDescription = null,
                        modifier = Modifier
                            .width(100.dp)
                            .height(100.dp)
                    )
                    Text(
                        text = listData[it].first,
                        fontSize = 25.sp,
                        modifier = Modifier.padding(start = 80.dp)
                    )
                }
            }
        }
    }
}

@Composable
fun DescribeText(text:String) {
    Text(text = text, color = Color.Black, textAlign = TextAlign.Center, modifier = Modifier.fillMaxWidth())
}

使用pullRefresh实现下拉加载与上拉加载

 

 

 

 

拖拽

 

end

 

标签:LazyHorizontalGrid,compose,listData,Jetpack,mipmap,fruit,ic,Modifier,dp
From: https://www.cnblogs.com/guanxinjing/p/17611791.html

相关文章

  • ubuntu安装docker和docker-compose
      ubuntu系统docker和安装,并安装docker-compose安装dockeraptinstalldocker.io设置docker为开机自启systemctlenabledocker安装docker-composeaptinstalldocker-compose修改配置,设置国内镜像仓库和本地镜像仓库vi/etc/docker/daemon.json{"registry-......
  • docker-compose快速部署elasticsearch-8.8.1集群+kibana+logstash
    安装环境centos7.98cpu16G内存vda50Gvdb100G如果您的环境是Linux,注意要做以下操作,否则es可能会启动失败用编辑工具打开文件/etc/sysctl.conf在尾部添加一行配置vm.max_map_count=262144,如果已存在就修改,数值不能低于262144修改保存,然后执行命令sudosysctl-p使其立即......
  • docker-compose 多项目部署
     DockerfileFROMnginxLABELcompony=com.ligyLABELby=ligenyunCOPYdist//usr/share/nginx/html/docker-compose.ymlversion:'3'services:ui2:container_name:ui2restart:alwaysbuild:./ui2ports:-"80......
  • CentOS安装podman-compose
    1.安装python3的依赖yum-yinstallzlib-develbzip2-developenssl-develncurses-develsqlite-develreadline-develtk-develgdbm-develdb4-devellibpcap-develxz-devellibffi-devel如果当前登录的是普通用户,需要在命令前加sudo,否则不用,下文同理。2.下载python3的......
  • Android开发 Jetpack Compose 与xml的混合开发AndroidView
    前言  JetpackCompose虽然已经逐渐完善,但是其实还是有很多地方未满足需求。比如播放视频、相机预览等等依然需要原来的View。所以目前阶段JetpackCompose与xml的混合开发非常重要。  官方文档地址:https://developer.android.google.cn/jetpack/compose/migrate/interopera......
  • Docker-Compose 一键部署mysql及初始化sql脚本
    1.部署前提:服务器具备docker和docker-compose环境2.镜像准备:-mysql:DockerFile文件:FROMmysql:5.7.41COPY*.sql/docker-entrypoint-initdb.d/初始化sql脚本xxx.sql放在DockerFile文件同级目录下执行指令:dockerbuild-fdockerfile_mysql-tmysql:1.0.0.......
  • docker-compose 启动MySQL
    version:'3.3'services:mysql:image:mysql:8.0.20##镜像restart:alwayshostname:mysqlcontainer_name:mysqlprivileged:trueports:-3306:3306environment:MYSQL_ROOT_PASSWORD:123456TZ:As......
  • docker compose 安装Prometheus+granfa
    cd/home/xxxxmkdir-pprometheuschmod777prometheuscdprometheusmkdir-pgrafana_dataprometheus_datachmod777grafana_dataprometheus_datadocker-compose.ymlversion:"3.7"services:node-exporter:image:prom/node-exporter:lat......
  • 记录一下【docker compose发布】 docker容器间通信
    踩坑:在网上找的帖子说是在dockercompose文件中的network下添加 1:直接报错, 2:然后又去查找说要在compose文件底部,和service同级添加network的声明,添加后还是不能通信,然后使用命令dockernetworkls查看发现新增了两个网络sub_test,pub_test 3:重新查找,使用已有网络需......
  • Android开发 Jetpack Compose Button
    前言  此篇博客讲解Button按钮一个简单的例子快速了解一下效果图代码@ComposablefunAPage(){Column(Modifier.fillMaxSize(),horizontalAlignment=Alignment.CenterHorizontally,verticalArrangement=Arrangement.Center){......