• Compose竖向列表LazyColumn


    基础列表一

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

    1. /**
    2. * 基础列表一
    3. */
    4. @Composable
    5. fun Items() {
    6. Box(modifier = Modifier.fillMaxSize()) {
    7. val context = LocalContext.current
    8. val dataList = arrayListOf<Int>()
    9. for (index in 1..50) {
    10. dataList.add(index)
    11. }
    12. val listState = rememberLazyListState()
    13. LazyColumn(state = listState) {
    14. items(dataList) { data ->
    15. Text(
    16. text = "第${data}条数据",
    17. textAlign = TextAlign.Center,
    18. //属性设置先后顺序不一样运行效果会不一样
    19. modifier = Modifier
    20. .fillMaxWidth()
    21. .padding(top = 1.dp)
    22. .background(Color.White)
    23. .clickable {
    24. Toast
    25. .makeText(context, "$data", Toast.LENGTH_SHORT)
    26. .show()
    27. }
    28. .padding(10.dp)
    29. )
    30. }
    31. }
    32. //返回顶部
    33. val coroutineScope = rememberCoroutineScope()
    34. Image(
    35. modifier = Modifier
    36. .padding(end = 10.dp, bottom = 10.dp)
    37. .width(45.dp)
    38. .height(45.dp)
    39. .clip(CircleShape)
    40. .align(Alignment.BottomEnd)
    41. .background(Color.Blue)
    42. .clickable {
    43. coroutineScope.launch {
    44. listState.animateScrollToItem(index = 0)
    45. }
    46. },
    47. painter = painterResource(id = R.drawable.top),
    48. contentDescription = "返回顶部图标"
    49. )
    50. }
    51. }

    基础列表二

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

    1. /**
    2. * 基础列表二
    3. */
    4. @Composable
    5. fun ItemsIndexed() {
    6. val context = LocalContext.current
    7. val stringList = arrayListOf()
    8. for (index in 1..50) {
    9. stringList.add(index.toString())
    10. }
    11. LazyColumn {
    12. //stringList.toArray()方法可以通过List的toArray方法将List转为Array
    13. itemsIndexed(stringList) { index, data ->
    14. Text(
    15. text = "我的下标是:${index},我的数据为:$data",
    16. textAlign = TextAlign.Center,
    17. modifier = Modifier
    18. .fillMaxWidth()
    19. .padding(top = 1.dp)
    20. .background(Color.White)
    21. .clickable {
    22. Toast
    23. .makeText(context, data, Toast.LENGTH_SHORT)
    24. .show()
    25. }
    26. )
    27. }
    28. }
    29. }

    多Type列表

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

    1. /**
    2. * 多Type列表
    3. */
    4. @Composable
    5. fun AnyTypeList() {
    6. val charList = arrayListOf()
    7. charList.apply {
    8. add(Chat("你好啊"))
    9. add(Chat("你在干啥呢"))
    10. add(Chat("想问你个事"))
    11. add(Chat("没干啥,还在写代码啊!", false))
    12. add(Chat("啥事啊大哥?", false))
    13. add(Chat("没事。。。"))
    14. add(Chat("好吧。。。", false))
    15. }
    16. LazyColumn {
    17. items(charList) { data ->
    18. if (data.isLeft) {
    19. Column(
    20. modifier = Modifier
    21. .fillMaxWidth()
    22. .padding(start = 10.dp)
    23. ) {
    24. //间隔设置
    25. Spacer(modifier = Modifier.height(10.dp))
    26. Row(
    27. verticalAlignment = Alignment.CenterVertically
    28. ) {
    29. Image(
    30. modifier = Modifier
    31. .width(35.dp)
    32. .height(35.dp)
    33. //裁剪圆形
    34. .clip(CircleShape),
    35. painter = painterResource(id = R.drawable.ic_launcher_background),
    36. contentDescription = "左头像"
    37. )
    38. Spacer(modifier = Modifier.width(10.dp))
    39. Text(
    40. data.content,
    41. modifier = Modifier
    42. .wrapContentWidth()
    43. .background(Color.Yellow)
    44. .padding(10.dp),
    45. )
    46. }
    47. }
    48. } else {
    49. Column(
    50. modifier = Modifier
    51. .fillMaxWidth()
    52. .padding(end = 10.dp),
    53. horizontalAlignment = Alignment.End
    54. ) {
    55. Spacer(modifier = Modifier.height(10.dp))
    56. Row(
    57. verticalAlignment = Alignment.CenterVertically
    58. ) {
    59. Text(
    60. data.content,
    61. modifier = Modifier
    62. .wrapContentWidth()
    63. .background(Color.Green)
    64. .padding(10.dp)
    65. )
    66. Spacer(modifier = Modifier.width(10.dp))
    67. Image(
    68. modifier = Modifier
    69. .width(35.dp)
    70. .height(35.dp)
    71. .clip(CircleShape),
    72. painter = painterResource(id = R.drawable.ic_launcher_background),
    73. contentDescription = "右头像"
    74. )
    75. }
    76. }
    77. }
    78. }
    79. }
    80. }

    数据类:

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

    粘性标题列表

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

    1. /**
    2. * 粘性标题列表
    3. */
    4. @OptIn(ExperimentalFoundationApi::class)
    5. @Composable
    6. fun StickyHeaderTest() {
    7. val context = LocalContext.current
    8. val letters = arrayListOf("类型一", "类型二", "类型三", "类型四", "类型五")
    9. val contactList = arrayListOf()
    10. val nameList = arrayListOf()
    11. for (index in 1..5) {
    12. nameList.add("子项$index")
    13. }
    14. for (index in letters.iterator()) {
    15. contactList.add(Contact(letters = index, nameList))
    16. }
    17. LazyColumn {
    18. contactList.forEach { (letter, nameList) ->
    19. stickyHeader {
    20. Text(
    21. text = letter,
    22. modifier = Modifier
    23. .background(Color.LightGray)
    24. .padding(start = 10.dp)
    25. .fillMaxWidth(),
    26. fontSize = 25.sp
    27. )
    28. }
    29. items(nameList) { contact ->
    30. Text(
    31. text = contact,
    32. modifier = Modifier
    33. .padding(10.dp)
    34. .background(Color.White)
    35. .fillMaxWidth()
    36. .clickable {
    37. Toast
    38. .makeText(context, contact, Toast.LENGTH_SHORT)
    39. .show()
    40. },
    41. textAlign = TextAlign.Center,
    42. fontSize = 25.sp
    43. )
    44. }
    45. }
    46. }
    47. }

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

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

    完整代码:

    1. import android.os.Bundle
    2. import android.widget.Toast
    3. import androidx.activity.ComponentActivity
    4. import androidx.activity.compose.setContent
    5. import androidx.compose.foundation.ExperimentalFoundationApi
    6. import androidx.compose.foundation.Image
    7. import androidx.compose.foundation.background
    8. import androidx.compose.foundation.clickable
    9. import androidx.compose.foundation.layout.Box
    10. import androidx.compose.foundation.layout.Column
    11. import androidx.compose.foundation.layout.Row
    12. import androidx.compose.foundation.layout.Spacer
    13. import androidx.compose.foundation.layout.fillMaxSize
    14. import androidx.compose.foundation.layout.fillMaxWidth
    15. import androidx.compose.foundation.layout.height
    16. import androidx.compose.foundation.layout.padding
    17. import androidx.compose.foundation.layout.width
    18. import androidx.compose.foundation.layout.wrapContentWidth
    19. import androidx.compose.foundation.lazy.LazyColumn
    20. import androidx.compose.foundation.lazy.items
    21. import androidx.compose.foundation.lazy.itemsIndexed
    22. import androidx.compose.foundation.lazy.rememberLazyListState
    23. import androidx.compose.foundation.shape.CircleShape
    24. import androidx.compose.material3.MaterialTheme
    25. import androidx.compose.material3.Surface
    26. import androidx.compose.material3.Text
    27. import androidx.compose.runtime.Composable
    28. import androidx.compose.runtime.rememberCoroutineScope
    29. import androidx.compose.ui.Alignment
    30. import androidx.compose.ui.Modifier
    31. import androidx.compose.ui.draw.clip
    32. import androidx.compose.ui.graphics.Color
    33. import androidx.compose.ui.platform.LocalContext
    34. import androidx.compose.ui.res.painterResource
    35. import androidx.compose.ui.text.style.TextAlign
    36. import androidx.compose.ui.tooling.preview.Preview
    37. import androidx.compose.ui.unit.dp
    38. import androidx.compose.ui.unit.sp
    39. import com.cwj.composedemo.ui.theme.ComposeDemoTheme
    40. import kotlinx.coroutines.launch
    41. class MainActivity : ComponentActivity() {
    42. override fun onCreate(savedInstanceState: Bundle?) {
    43. super.onCreate(savedInstanceState)
    44. setContent {
    45. ComposeDemoTheme {
    46. // A surface container using the 'background' color from the theme
    47. Surface(
    48. modifier = Modifier.fillMaxSize(),
    49. color = MaterialTheme.colorScheme.background
    50. ) {
    51. Greeting()
    52. }
    53. }
    54. }
    55. }
    56. }
    57. @Composable
    58. fun Greeting() {
    59. // Items()
    60. // ItemsIndexed()
    61. // AnyTypeList()
    62. StickyHeaderTest()
    63. }
    64. /**
    65. * 粘性标题列表
    66. */
    67. @OptIn(ExperimentalFoundationApi::class)
    68. @Composable
    69. fun StickyHeaderTest() {
    70. val context = LocalContext.current
    71. val letters = arrayListOf("类型一", "类型二", "类型三", "类型四", "类型五")
    72. val contactList = arrayListOf()
    73. val nameList = arrayListOf()
    74. for (index in 1..5) {
    75. nameList.add("子项$index")
    76. }
    77. for (index in letters.iterator()) {
    78. contactList.add(Contact(letters = index, nameList))
    79. }
    80. LazyColumn {
    81. contactList.forEach { (letter, nameList) ->
    82. stickyHeader {
    83. Text(
    84. text = letter,
    85. modifier = Modifier
    86. .background(Color.LightGray)
    87. .padding(start = 10.dp)
    88. .fillMaxWidth(),
    89. fontSize = 25.sp
    90. )
    91. }
    92. items(nameList) { contact ->
    93. Text(
    94. text = contact,
    95. modifier = Modifier
    96. .padding(10.dp)
    97. .background(Color.White)
    98. .fillMaxWidth()
    99. .clickable {
    100. Toast
    101. .makeText(context, contact, Toast.LENGTH_SHORT)
    102. .show()
    103. },
    104. textAlign = TextAlign.Center,
    105. fontSize = 25.sp
    106. )
    107. }
    108. }
    109. }
    110. }
    111. /**
    112. * 多Type列表
    113. */
    114. @Composable
    115. fun AnyTypeList() {
    116. val charList = arrayListOf()
    117. charList.apply {
    118. add(Chat("你好啊"))
    119. add(Chat("你在干啥呢"))
    120. add(Chat("想问你个事"))
    121. add(Chat("没干啥,还在写代码啊!", false))
    122. add(Chat("啥事啊大哥?", false))
    123. add(Chat("没事。。。"))
    124. add(Chat("好吧。。。", false))
    125. }
    126. LazyColumn {
    127. items(charList) { data ->
    128. if (data.isLeft) {
    129. Column(
    130. modifier = Modifier
    131. .fillMaxWidth()
    132. .padding(start = 10.dp)
    133. ) {
    134. //间隔设置
    135. Spacer(modifier = Modifier.height(10.dp))
    136. Row(
    137. verticalAlignment = Alignment.CenterVertically
    138. ) {
    139. Image(
    140. modifier = Modifier
    141. .width(35.dp)
    142. .height(35.dp)
    143. //裁剪圆形
    144. .clip(CircleShape),
    145. painter = painterResource(id = R.drawable.ic_launcher_background),
    146. contentDescription = "左头像"
    147. )
    148. Spacer(modifier = Modifier.width(10.dp))
    149. Text(
    150. data.content,
    151. modifier = Modifier
    152. .wrapContentWidth()
    153. .background(Color.Yellow)
    154. .padding(10.dp),
    155. )
    156. }
    157. }
    158. } else {
    159. Column(
    160. modifier = Modifier
    161. .fillMaxWidth()
    162. .padding(end = 10.dp),
    163. horizontalAlignment = Alignment.End
    164. ) {
    165. Spacer(modifier = Modifier.height(10.dp))
    166. Row(
    167. verticalAlignment = Alignment.CenterVertically
    168. ) {
    169. Text(
    170. data.content,
    171. modifier = Modifier
    172. .wrapContentWidth()
    173. .background(Color.Green)
    174. .padding(10.dp)
    175. )
    176. Spacer(modifier = Modifier.width(10.dp))
    177. Image(
    178. modifier = Modifier
    179. .width(35.dp)
    180. .height(35.dp)
    181. .clip(CircleShape),
    182. painter = painterResource(id = R.drawable.ic_launcher_background),
    183. contentDescription = "右头像"
    184. )
    185. }
    186. }
    187. }
    188. }
    189. }
    190. }
    191. /**
    192. * 基础列表二
    193. */
    194. @Composable
    195. fun ItemsIndexed() {
    196. val context = LocalContext.current
    197. val stringList = arrayListOf()
    198. for (index in 1..50) {
    199. stringList.add(index.toString())
    200. }
    201. LazyColumn {
    202. //stringList.toArray()方法可以通过List的toArray方法将List转为Array
    203. itemsIndexed(stringList) { index, data ->
    204. Text(
    205. text = "我的下标是:${index},我的数据为:$data",
    206. textAlign = TextAlign.Center,
    207. modifier = Modifier
    208. .fillMaxWidth()
    209. .padding(top = 1.dp)
    210. .background(Color.White)
    211. .clickable {
    212. Toast
    213. .makeText(context, data, Toast.LENGTH_SHORT)
    214. .show()
    215. }
    216. )
    217. }
    218. }
    219. }
    220. /**
    221. * 基础列表一
    222. */
    223. @Composable
    224. fun Items() {
    225. Box(modifier = Modifier.fillMaxSize()) {
    226. val context = LocalContext.current
    227. val dataList = arrayListOf<Int>()
    228. for (index in 1..50) {
    229. dataList.add(index)
    230. }
    231. val listState = rememberLazyListState()
    232. LazyColumn(state = listState) {
    233. items(dataList) { data ->
    234. Text(
    235. text = "第${data}条数据",
    236. textAlign = TextAlign.Center,
    237. //属性设置先后顺序不一样运行效果会不一样
    238. modifier = Modifier
    239. .fillMaxWidth()
    240. .padding(top = 1.dp)
    241. .background(Color.White)
    242. .clickable {
    243. Toast
    244. .makeText(context, "$data", Toast.LENGTH_SHORT)
    245. .show()
    246. }
    247. .padding(10.dp)
    248. )
    249. }
    250. }
    251. //返回顶部
    252. val coroutineScope = rememberCoroutineScope()
    253. Image(
    254. modifier = Modifier
    255. .padding(end = 10.dp, bottom = 10.dp)
    256. .width(45.dp)
    257. .height(45.dp)
    258. .clip(CircleShape)
    259. .align(Alignment.BottomEnd)
    260. .background(Color.Blue)
    261. .clickable {
    262. coroutineScope.launch {
    263. listState.animateScrollToItem(index = 0)
    264. }
    265. },
    266. painter = painterResource(id = R.drawable.top),
    267. contentDescription = "返回顶部图标"
    268. )
    269. }
    270. }
    271. @Preview(showBackground = true, showSystemUi = true)
    272. @Composable
    273. fun GreetingPreview() {
    274. ComposeDemoTheme {
    275. Greeting()
    276. }
    277. }

  • 相关阅读:
    【Linux】命令
    Vue学习——Vue-Cli创建Vue项目(19)
    简单的个人博客网站设计 静态HTML个人博客主页 DW个人网站模板下载 大学生简单个人网页作品代码 个人网页制作 学生个人网页设计作业
    集合框架(二)前置知识
    Doris学习笔记之优化
    python版超市信息管理系统源代码,基于tkinter带界面
    『手撕Vue-CLI』自动安装依赖
    vue3使用vue-virtual-scroller虚拟滚动遇到的问题
    String类 - 下篇
    国内AI绘画软件“数画”的相机溯源码功能太强大,彻底消灭盗图
  • 原文地址:https://blog.csdn.net/juer2017/article/details/133908983