【Android Compose】ListView效果

embedded/2024/10/18 16:53:51/

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

Compose__17">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.ppmy.cn/embedded/85617.html

相关文章

动态接口调优:在Mojo模型中调整模型的输入输出接口

动态接口调优&#xff1a;在Mojo模型中调整模型的输入输出接口 在机器学习领域&#xff0c;Mojo模型通常指代一个经过训练、准备部署的模型。模型的输入输出接口&#xff08;I/O&#xff09;是模型与外界交互的桥梁&#xff0c;其设计直接影响到模型的可用性和灵活性。本文将探…

【技术追踪】基于扩散模型的医学图像合成与测量指导(TPAMI-2024)

不确定性引导条件&#xff1a;从生成类别医学图像到生成没那么确定的类别医学图像&#xff0c;增加合成图像的信息量~ 论文&#xff1a;Measurement Guidance in Diffusion Models: Insight from Medical Image Synthesis 代码&#xff1a;https://github.com/yangqy1110/MGDM …

【CTFWP】ctfshow-web32

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 题目介绍&#xff1a;题目分析&#xff1a;payload&#xff1a;payload解释&#xff1a;flag 题目介绍&#xff1a; <?php/* # -*- coding: utf-8 -*- # Autho…

理解 Kotlin 中的 crossinline 关键字

理解 Kotlin 中的 crossinline 关键字 Kotlin 提供了丰富的功能&#xff0c;用于开发简洁且富有表现力的代码。这些特性包括高阶函数和 Lambda 表达式&#xff0c;它们是 Kotlin 设计的核心部分。在使用这些构造时&#xff0c;您可能会遇到 crossinline 关键字。在本文中&#…

ASP.NET MVC

ASP.NET MVC与.NET Framework关系 .NET Framework是一个庞大的代码库&#xff0c;能为多种编程语言提供支持(如C#、VB、F#等)。同时.NET Framework 提供了多种技术框架&#xff0c;ASP.NET MVC是.NET Framework提供的众多技术框架中的一种&#xff0c;用于开发Web应用。 .NET …

leetcode日记(47)螺旋矩阵Ⅱ

这题思路不难&#xff0c;就是找规律太难了。 我首先的思路是一行一行来&#xff0c;根据规律填入下一行的数组&#xff0c;第i行是由前i个数字&#xff08;n-2*i&#xff09;个增序数列后i个数字组成&#xff0c;后来觉得太难找规律了就换了一种思路。 思路大致是先计算出需…

google 浏览器插件开发简单学习案例:TodoList;打包成crx离线包

参考&#xff1a; google插件支持&#xff1a; https://blog.csdn.net/weixin_42357472/article/details/140412993 这里是把前面做的TodoList做成google插件&#xff0c;具体网页可以参考下面链接 TodoList网页&#xff1a; https://blog.csdn.net/weixin_42357472/article/de…

Linux的yum源安装MySQL5.7

linux的yum源安装MySQL5.7 一、MySQL 1、简介 MySQL 是一种流行的关系型数据库管理系统&#xff08;RDBMS&#xff09;&#xff0c;由瑞典公司 MySQL AB 开发&#xff0c;后来被 Oracle Corporation 收购。它是一个开源软件&#xff0c;提供了高效、稳定和可靠的数据管理解决…