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

JetpackCompose从入门到实战学习笔记13——Compose中实现简单瀑布流

JetpackCompose从入门到实战学习笔记13——Compose中实现简单瀑布流

1.简介:

LazyGrid包含两种控件:LazyVerticalStaggeredGrid和LazyHorizontalStaggeredGrid。两者内部均由LazyLayout实现(包括LazyColumn和LazyRow也是由LazyLayout实现的)。今天先以LazyVerticalStaggeredGrid为例。

2.竖向的StaggeredGrid参数:

参数描述
cell布局item的个数
modifier布局的基本修饰
state滑动时的状态
contentPadding布局内容的外间距
verticalArrangement布局子级的水平布置
horizontalArrangement布局子级的 垂直 对其方式,默认从布局顶部对齐(同Row相关参数的含义)
flingBehavior指定滑动时的行为
userScrollEnabled是否可滑动,默认为true
content布局item的具体内容
@ExperimentalFoundationApi
@Composable
fun LazyVerticalStaggeredGrid(
    columns: StaggeredGridCells,
    modifier: Modifier = Modifier,
    state: LazyStaggeredGridState = rememberLazyStaggeredGridState(),
    contentPadding: PaddingValues = PaddingValues(0.dp),
    verticalArrangement: Arrangement.Vertical = Arrangement.spacedBy(0.dp),
    horizontalArrangement: Arrangement.Horizontal = Arrangement.spacedBy(0.dp),
    flingBehavior: FlingBehavior = ScrollableDefaults.flingBehavior(),
    userScrollEnabled: Boolean = true,
    content: LazyStaggeredGridScope.() -> Unit
) {
    LazyStaggeredGrid(
        modifier = modifier,
        orientation = Orientation.Vertical,
        state = state,
        verticalArrangement = verticalArrangement,
        horizontalArrangement = horizontalArrangement,
        contentPadding = contentPadding,
        flingBehavior = flingBehavior,
        userScrollEnabled = userScrollEnabled,
        slotSizesSums = rememberColumnWidthSums(columns, horizontalArrangement, contentPadding),
        content = content
    )
}

3.简单使用如下:

    @OptIn(ExperimentalFoundationApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent() {
            LazyStaggeredGridComposeTheme() {
                LazyVerticalStaggeredGrid(
                    columns = StaggeredGridCells.Adaptive(110.dp),
                    modifier = Modifier.fillMaxSize(),
                    state = rememberLazyStaggeredGridState(),
                    contentPadding = PaddingValues(16.dp),
                    horizontalArrangement = Arrangement.spacedBy(16.dp),
                    verticalArrangement = Arrangement.spacedBy(16.dp),
                    flingBehavior = ScrollableDefaults.flingBehavior(),
                ) {
                    items(items) { item ->
                        RandomColorBox(item = item)
                    }
                }
            }
        }
    }

4.竖向瀑布流效果如下:

竖屏下的效果:
在这里插入图片描述

横屏下的效果:
在这里插入图片描述
在这里插入图片描述

5.横向的StaggeredGrid参数:

rows布局item的个数
modifier布局的基本修饰
state滑动时的状态
contentPadding布局内容的外间距
verticalArrangement布局子级的水平布置
horizontalArrangement布局子级的 垂直 对其方式,默认从布局顶部对齐(同Row相关参数的含义)
flingBehavior指定滑动时的行为
参数描述
userScrollEnabled是否可滑动,默认为true
content布局item的具体内容
@ExperimentalFoundationApi
@Composable
fun LazyHorizontalStaggeredGrid(
    rows: StaggeredGridCells,
    modifier: Modifier = Modifier,
    state: LazyStaggeredGridState = rememberLazyStaggeredGridState(),
    contentPadding: PaddingValues = PaddingValues(0.dp),
    verticalArrangement: Arrangement.Vertical = Arrangement.spacedBy(0.dp),
    horizontalArrangement: Arrangement.Horizontal = Arrangement.spacedBy(0.dp),
    flingBehavior: FlingBehavior = ScrollableDefaults.flingBehavior(),
    userScrollEnabled: Boolean = true,
    content: LazyStaggeredGridScope.() -> Unit
) {
    LazyStaggeredGrid(
        modifier = modifier,
        orientation = Orientation.Horizontal,
        state = state,
        contentPadding = contentPadding,
        verticalArrangement = verticalArrangement,
        horizontalArrangement = horizontalArrangement,
        flingBehavior = flingBehavior,
        userScrollEnabled = userScrollEnabled,
        slotSizesSums = rememberRowHeightSums(rows, verticalArrangement, contentPadding),
        content = content
    )
}

6.简单使用如下:

@Preview
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun LazyHorizontalStaggeredGridDemo() {
    LazyHorizontalStaggeredGrid(
        rows = StaggeredGridCells.Adaptive(110.dp),
        modifier = Modifier.fillMaxSize(),
        state = rememberLazyStaggeredGridState(),
        contentPadding = PaddingValues(16.dp),
        horizontalArrangement = Arrangement.spacedBy(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp),
        flingBehavior = ScrollableDefaults.flingBehavior(),
        userScrollEnabled = true,//瀑布流是否可滑动
    )
    {
        items(items) { item ->
            HorizontalRandomColorBox(item = item)
        }
    }
}

7.横向瀑布流效果如下:

横屏效果:
在这里插入图片描述

竖向效果:
在这里插入图片描述

在这里插入图片描述

8.完整代码如下:

package com.example.lazystaggedgrildcompose

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.ScrollableDefaults
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.staggeredgrid.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.example.lazystaggedgrildcompose.ui.theme.LazyStaggeredGridComposeTheme
import kotlin.random.Random

class MainActivity : ComponentActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent() {
            LazyStaggeredGridComposeTheme {
                //竖向瀑布流
               // LazyVerticalStaggeredGridDemo()
                //横向瀑布流
                LazyHorizontalStaggeredGridDemo()
            }
        }
    }
}

@Preview
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun LazyVerticalStaggeredGridDemo() {
    LazyVerticalStaggeredGrid(
        columns = StaggeredGridCells.Adaptive(110.dp),
        modifier = Modifier.fillMaxSize(),
        state = rememberLazyStaggeredGridState(),
        contentPadding = PaddingValues(16.dp),
        horizontalArrangement = Arrangement.spacedBy(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp),
        flingBehavior = ScrollableDefaults.flingBehavior(),
        userScrollEnabled = true,//瀑布流是否可滑动
    ) {
        items(items) { item ->
            VerticalRandomColorBox(item = item)
        }
    }
}

@Preview
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun LazyHorizontalStaggeredGridDemo() {
    LazyHorizontalStaggeredGrid(
        rows = StaggeredGridCells.Adaptive(110.dp),
        modifier = Modifier.fillMaxSize(),
        state = rememberLazyStaggeredGridState(),
        contentPadding = PaddingValues(16.dp),
        horizontalArrangement = Arrangement.spacedBy(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp),
        flingBehavior = ScrollableDefaults.flingBehavior(),
        userScrollEnabled = true,//瀑布流是否可滑动
    )
    {
        items(items) { item ->
            HorizontalRandomColorBox(item = item)
        }
    }
}

data class ListItem(
    val height: Dp,
    val color: Color
)

var items = (1..100).map {
    ListItem(
        height = Random.nextInt(100, 300).dp,
        color = Color(Random.nextLong(0XFFFFFF))
            .copy(alpha = 1f)
    )
}

@Composable
fun VerticalRandomColorBox(item: ListItem) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .height(item.height)
            .clip(RoundedCornerShape(10.dp))
            .background(item.color)
    )
}

@Composable
fun HorizontalRandomColorBox(item: ListItem) {
    Box(
        modifier = Modifier
            .width(200.dp)//宽度可以随意调整
            .fillMaxHeight()
            .clip(RoundedCornerShape(10.dp))
            .background(item.color)
    )
}

9.项目demo的源码地址如下:

https://gitee.com/jackning_admin/lazy-stagged-grid-compose-demo

10.总结:

从上面的例子中可以看出:compose实现一个瀑布流demo非常简单方便,横向和竖向代码都是20行左右,道险且长,仍需奋勇前行!!!休息了,各位大侠!!


http://www.kler.cn/a/7373.html

相关文章:

  • ubuntu 安装kafka-eagle
  • WP网站如何增加文章/页面的自定义模板
  • 2024山西省网络建设运维第十八届职业院校技能大赛解析答案(5. nginx 和 tomcat 服务)
  • C函数如何返回参数lua使用
  • 27-压力测试
  • Electron 沙盒模式与预加载脚本:保障桌面应用安全的关键机制
  • 基于html+css的内容旋转
  • Solon v2.2.7 发布,支持 Java 20
  • MLSkin 5.3 for delphi Crack
  • 约瑟夫问题
  • 【redis】redis分布式锁
  • 镜头光学指标介绍----清晰度SFR
  • 【从零开始学习 UVM】10.2、UVM TLM —— UVM TLM Blocking Put Port
  • 【CSAPP】进程 | 上下文切换 | 用户视角下的并发进程
  • 流量整形(GTS和LR)
  • 蓝桥杯之单片机学习(终)——关于之前文章的错误及更正(附:第十四届蓝桥杯单片机赛题)
  • L2-040 哲哲打游戏 简单模拟
  • 免费CRM如何进行选择?
  • 用GPT-4写代码不用翻墙了?Cursor告诉你:可以~~
  • 【视频分割】【深度学习】MiVOS官方Pytorch代码-S2M模块DeepLavV3Plus网络解析
  • 【Vue框架】Vue绑定样式及案例之行内样式——对象绑定样式与数组控制样式(附带源码案例)
  • 前端基础-ES6
  • 网络安全行业现在好混吗,工资水平怎么样?
  • Junit 5 单元测试框架
  • Matlab 一种计算植物面积密度的新方法(论文复现:凸包法)
  • 【C++】用一棵红黑树同时封装出map和set