当前位置: 首页 > news >正文

【Android Compose】ListView效果

【Android Compose】ListView效果

  • 1、Column、Row 和 Box
  • 2、LazyColumn和LazyRow
  • 3、Compose 中的状态
  • 4、ListView效果
  • 5、android-compose-codelabs

Jetpack Compose 使用入门
Jetpack Compose 教程
Jetpack Compose


1、Column、Row 和 Box

Compose 中的三个基本标准布局元素是 Column、Row 和 Box 可组合项。

在这里插入图片描述

2、LazyColumn和LazyRow

androidx.compose.foundation.lazy.LazyColumn
androidx.compose.foundation.lazy.LazyRow
androidx.compose.foundation.lazy.items

3、Compose 中的状态

7. Compose 中的状态

  • mutableStateOf 函数,该函数可让 Compose 重组读取该 State 的函数
  • 如需在重组后保留状态,请使用 remember 记住可变状态
  • onClick 它不接受值,而接受函数
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
// ...@Composable
fun Greeting(...) {val expanded = remember { mutableStateOf(false) }ElevatedButton(onClick = { expanded.value = !expanded.value },) {Text(if (expanded.value) "Show less" else "Show more")}
}

4、ListView效果

https://gitee.com/xhbruce/XhAndroidDemo
在这里插入图片描述

package com.xhbruce.xhandroiddemoimport Message
import SampleData
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.view.WindowCompat
import com.xhbruce.xhandroiddemo.ui.theme.XhAndroidDemoThemeclass MainActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)enableEdgeToEdge()WindowCompat.setDecorFitsSystemWindows(window, true)setContent {XhAndroidDemoTheme {//PreviewGreeting()Conversation(SampleData.conversationSample)}}}
}@Composable
fun MessageCard(msg: Message) {// We keep track if the message is expanded or not in this// variablevar isExpanded by remember { mutableStateOf(false) }// surfaceColor will be updated gradually from one color to the otherval surfaceColor by animateColorAsState(if (isExpanded)MaterialTheme.colorScheme.primaryelseMaterialTheme.colorScheme.surface,)// We toggle the isExpanded variable when we click on this ColumnColumn(modifier = Modifier.clickable { isExpanded = !isExpanded }.fillMaxSize()) {Text(text = msg.author,color = MaterialTheme.colorScheme.secondary,style = MaterialTheme.typography.titleSmall)Spacer(modifier = Modifier.height(4.dp))Surface(shape = MaterialTheme.shapes.medium,shadowElevation = 1.dp,// surfaceColor color will be changing gradually from primary to surfacecolor = surfaceColor,// animateContentSize will change the Surface size graduallymodifier = Modifier.animateContentSize().padding(1.dp)) {Text(text = msg.body,modifier = Modifier.padding(all = 4.dp),// If the message is expanded, we display all its content// otherwise we only display the first linemaxLines = if (isExpanded) Int.MAX_VALUE else 1,style = MaterialTheme.typography.bodyMedium)}}
}@Composable
fun Conversation(messages: List<Message>) {LazyColumn {items(messages) { message ->MessageCard(message)}}
}@Preview
@Composable
fun PreviewMessageCard() {XhAndroidDemoTheme {Conversation(SampleData.conversationSample)}
}@Preview
@Composable
fun PreviewGreeting() {val expanded = remember { mutableStateOf(false) }ElevatedButton(onClick = { expanded.value = !expanded.value },) {Text(if (expanded.value) "Show less" else "Show more")}
}

5、android-compose-codelabs

git clone https://github.com/googlecodelabs/android-compose-codelabs.git

在这里插入图片描述

http://www.lryc.cn/news/407472.html

相关文章:

  • 【Pytorch实战教程】Pytorch中.detach()的详细介绍
  • AR 眼镜之-充电动画定制-实现方案
  • AJAX-XMLHttpRequest 详解
  • 内容管理系统 Contentful 与 Baklib
  • [Mysql-视图和存储过程]
  • Linux下C++静态链接库的生成以及使用
  • 【8月EI会议推荐】第四届区块链技术与信息安全国际会议
  • 2024年【甘肃省安全员B证】考试资料及甘肃省安全员B证模拟试题
  • 结合el-upload上传组件,验证文件格式及大小
  • 配置php-fpm服务
  • 科普文:Linux系统安全加固指南
  • MFC开发,自定义消息
  • 如何在 SpringBoot 中优雅的做参数校验?
  • Godot入门 03世界构建1.0版
  • GitHub每日最火火火项目(7.26)
  • 微服务实践和总结
  • Spring Boot中的策略模式:优雅地处理不同商品类型的订单
  • django_创建菜单(实现整个项目的框架,调包)
  • 最新全新UI异次元荔枝V4.4自动发卡系统源码
  • PyTorch安装CUDA标准流程(可解决大部分GPU无法使用问题)
  • C++从入门到起飞之——初始化列表类型转换static成员 全方位剖析!
  • PHP框架简介
  • 微信小程序-粘性组件
  • 微服务注册中心
  • HDU1032——The 3n + 1 problem,HDU1033——Edge,HDU1034——Candy Sharing Game
  • 内网对抗-隧道技术篇防火墙组策略HTTP反向SSH转发出网穿透CrossC2解决方案
  • 实战案例:如何用ChatGPT生成适合不同领域的高质量文章
  • 多线程案例-单例模式
  • P6 优化篇 - 数据折线图可视化步骤
  • 优选算法之二分查找(上)