HarmonyOS Next应用开发——抽屉布局SideBarContainer

server/2024/10/17 3:02:24/

抽屉布局SideBarContainer

提供侧边栏可以显示和隐藏的侧边栏容器,通过子组件定义侧边栏和内容区,第一个子组件表示侧边栏,第二个子组件表示内容区。

并且侧边栏可以出现在左侧也可以出现在右侧,侧边栏可以并列跟内容区一起展示,也可以浮动在内容区展示。

在这里插入图片描述

常用属性
showSideBar(value: boolean)

设置是否显示侧边栏。注意:必须使用$$装饰,否则无效。

showControlButton(value: boolean)

设置是否显示控制按钮。

controlButton(value: ButtonStyle)

设置控制按钮样式。

sideBarWidth(value: number)

设置侧边栏宽度,默认240vp。

SideBarContainer( type?: SideBarContainerType )
名称描述
Embed侧边栏嵌入到组件内,和内容区并列显示。组件尺寸小于minContentWidth + minSideBarWidth,并且未设置showSideBar时,侧边栏自动隐藏。未设置minSideBarWidth或者minContentWidth采用未设置接口的默认值进行计算。组件在自动隐藏后,如果通过点击控制按钮唤出侧边栏,则侧边栏悬浮在内容区上显示。
Overlay侧边栏浮在内容区上面。
AUTO10+组件尺寸大于等于minSideBarWidth+minContentWidth时,采用Embed模式显示。组件尺寸小于minSideBarWidth+minContentWidth时,采用Overlay模式显示。未设置minSideBarWidth或minContentWidth时,会使用未设置接口的默认值进行计算,若计算的值小于600vp,则使用600vp做为模式切换的断点值。
开发步骤:

构建侧边栏:

Column() { //侧边栏内容区默认宽度240vpForEach(this.datas, (item: Data, index: number) => {ListItem() {Text(item.txt).fontSize(24).fontWeight(FontWeight.Bold).textAlign(TextAlign.Center).border({ width: 2, style: BorderStyle.Dotted }).width('100%').padding(20).onClick(() => { //点击列表项this.ckitem = index //更新点击下标 进而重新渲染内容区背景颜色animateTo({curve:curves.springMotion()},()=>{this.isshowing = false}) //动画关闭侧边栏})}}, (item: Data) => JSON.stringify(item))
}.width('100%')
.height('100%')
.backgroundColor(Color.Gray)
.borderRadius(10)

构建内容区:

Column() { //内容区Text(this.datas[this.ckitem].txt).fontSize(30).fontWeight(FontWeight.Bold)
}.width('100%')
.height('100%')
.backgroundColor(this.datas[this.ckitem].color)
.justifyContent(FlexAlign.Center)
.onTouch((event) => { //监听手指右边滑动和点击事件if (event.type == TouchType.Move) {this.movex = event.touches[0].xif ((this.movex - this.downx) > 50) { //产生了右侧滑动 打开抽屉animateTo({curve:curves.springMotion()},()=>{this.isshowing = true})}} else if (event.type == TouchType.Down) {this.downx = event.touches[0].x// 点击内容区就会关闭抽屉animateTo({curve:curves.springMotion()},()=>{this.isshowing = false})}
})

注意: 我们通过切换控制变量来控制侧边栏是否显示,为了显示出抽屉效果,我们给变量的变化都加入了动画。并且组件默认不支持手指滑动显示或关闭侧边栏,所以我们给内容区加入了手指右侧滑动的监听来自定义效果。

完整的代码:
import { curves } from '@kit.ArkUI';interface Data {color: Colortxt: string
}@Entry
@Component
struct SlideBarContainerPage {// 侧边栏数据源@State datas: Data[] = []// 点击item@State ckitem: number = 0// 侧边栏显示控制器@State isshowing: boolean = false// 手指按下x坐标private downx: number = 0// 手指移动x坐标private movex: number = 0aboutToAppear(): void {this.datas = [//数据源{txt: '白色',color: Color.White},{txt: '黑色',color: Color.Black},{txt: '红色',color: Color.Red},{txt: '绿色',color: Color.Green},{txt: '蓝色',color: Color.Blue},]}build() {SideBarContainer(SideBarContainerType.Overlay) {Column() { //侧边栏内容区默认宽度240vpForEach(this.datas, (item: Data, index: number) => {ListItem() {Text(item.txt).fontSize(24).fontWeight(FontWeight.Bold).textAlign(TextAlign.Center).border({ width: 2, style: BorderStyle.Dotted }).width('100%').padding(20).onClick(() => { //点击列表项this.ckitem = index //更新点击下标 进而重新渲染内容区背景颜色animateTo({curve:curves.springMotion()},()=>{this.isshowing = false}) //动画关闭侧边栏})}}, (item: Data) => JSON.stringify(item))}.width('100%').height('100%').backgroundColor(Color.Gray).borderRadius(10)Column() { //内容区Text(this.datas[this.ckitem].txt).fontSize(30).fontWeight(FontWeight.Bold)}.width('100%').height('100%').backgroundColor(this.datas[this.ckitem].color).justifyContent(FlexAlign.Center).onTouch((event) => { //监听手指右边滑动和点击事件if (event.type == TouchType.Move) {this.movex = event.touches[0].xif ((this.movex - this.downx) > 50) { //产生了右侧滑动 打开抽屉animateTo({curve:curves.springMotion()},()=>{this.isshowing = true})}} else if (event.type == TouchType.Down) {this.downx = event.touches[0].x// 点击内容区就会关闭抽屉animateTo({curve:curves.springMotion()},()=>{this.isshowing = false})}})}.height('100%').width('100%').showSideBar($$this.isshowing) //双向绑定 控制抽屉的开和关}
}

http://www.ppmy.cn/server/131359.html

相关文章

H7-TOOL的LUA小程序教程第14期:任意波形信号发生器,0-20mA输出和微型数控电源(2024-10-11,已更新)

LUA脚本的好处是用户可以根据自己注册的一批API(当前TOOL已经提供了几百个函数供大家使用),实现各种小程序,不再限制Flash里面已经下载的程序,就跟手机安装APP差不多,所以在H7-TOOL里面被广泛使用&#xff…

数通--3

一、动态路由 内部 路由器之间要互联互通,必须遵循相同的协议 企业内部用 IGP,企业之间用BGP RIP(已淘汰,不考) 距离就是长短,矢量就是方向,即路由的出接口 一台路由器 A 配好RIP,…

解析 wxPython 和 Pandas 实现的 XLSX 分析器和网页打开器

在本文中,我们将分析一个使用 wxPython 和 Pandas 库编写的 Python 应用程序,名为 “XLSX Analyzer and Web Opener”。该应用程序的核心功能是:从 Excel 文件中读取数据并显示在网格中,此外,还允许用户使用 Google Ch…

天气API接口调用

天气API接口: 天气API接口是一种用于获取实时或预报天气信息的应用程序编程接口(API)。开发者可以使用这种接口在他们的应用程序或网站上集成天气查询功能,比如查询某个地区的当前温度、降水量、风速等数据。 通常,你…

C#-使用Serilog框架快速实现日志及其相关扩展

目录 一、Serilog日志实现 1、实现 ILogEventSink接口 2、日志类Log 3、日志级别LogLevel 4、ILogger接口 5、日志服务实现 6、日志视图View 7、ViewModel 二、功能扩展 1、日志扩展方法 2、Trace追踪扩展日志 3、自动滚动至底部 一、Serilog日志实现 安装NuGet包…

每天一个数据分析题(五百零四)- 抽取样本

下列哪种方法,会重复抽取训练数据集中的数据,且每笔被抽中的概率始终保持一样? A. 袋装法(Bagging) B. 提升法(Boosting) C. 支持向量机(SVM) D. 以上皆是 数据分析…

Pyppeteer:如何在 Python 中使用 Puppeteer 和 Browserless?

Python 中的 Pyppeteer 是什么? Pyppeteer 是流行的 Node.js 库 Puppeteer 的 Python 移植版本,用于以编程方式控制无头 Chrome 或 Chromium 浏览器。 本质上,Pyppeteer 允许 Python 开发人员在 Web 浏览器中自动执行任务,例如抓…

Python OpenCV精讲系列 - 三维重建深入理解(十七)

💖💖⚡️⚡️专栏:Python OpenCV精讲⚡️⚡️💖💖 本专栏聚焦于Python结合OpenCV库进行计算机视觉开发的专业教程。通过系统化的课程设计,从基础概念入手,逐步深入到图像处理、特征检测、物体识…