温馨提示:本篇博客的详细代码已发布到 git : https://gitcode.com/nutpi/HarmonyosNext 可以下载运行哦!
项目运行效果示例如下:
UVList组件是一个列表容器组件,负责整体布局和列表项的渲染。下面是UVList组件的完整实现代码:
// UVList.ets
import { ListProps,ListItemProps } from '../common/interfaces';
import UVListItem from './UVListItem';
@Component
struct UVList {
// 列表属性
private props: ListProps ={} as ListProps;
build() {
Column() {
// 列表标题(如果有)
if (this.props.title) {
Text(this.props.title)
.fontSize(18)
.fontWeight(FontWeight.Bold)
.width('100%')
.textAlign(TextAlign.Start)
.padding({ left: 16, right: 16, top: 16, bottom: 8 })
}
// 列表内容
List() {
// 使用ForEach遍历列表项数据
ForEach(this.props.items, (item:ListItemProps) => {
ListItem() {
// 使用列表项组件
UVListItem({
item: item,
showThumbnail: this.props.showThumbnail ?? true,
showIcon: this.props.showIcon ?? true,
showRightText: this.props.showRightText ?? true,
navigable: this.props.navigable ?? true
})
}
.padding({ left: 16, right: 16, top: 4, bottom: 4 })
})
}
.width('100%')
.divider({ strokeWidth: 1, color: '#f5f5f5', startMargin: 16, endMargin: 16 })
.borderRadius(8)
.backgroundColor(Color.White)
}
.width('100%')
}
}
export default UVList;
属性 | 类型 | 说明 |
---|---|---|
props | ListProps | 列表组件的配置属性,包含标题、列表项数据等 |
UVList组件的渲染流程如下:
UVListItem组件负责单个列表项的内容展示和交互。下面是UVListItem组件的核心实现:
import { ListItemProps } from "../common/interfaces";
@Component
struct UVListItem {
// 列表项属性
private item: ListItemProps ={} as ListItemProps;
// 是否显示缩略图
private showThumbnail: boolean = true;
// 是否显示图标
private showIcon: boolean = true;
// 是否显示右侧文本
private showRightText: boolean = true;
// 是否可导航(显示右侧箭头)
private navigable: boolean = true;
build() {
Row() {
// 左侧图标区域
if (this.showIcon && this.item.icon) {
Row() {
// 根据图标类型显示不同的内容
if (typeof this.item.icon === 'string') {
// 如果是字符串类型,显示文本作为图标
Text(this.item.icon)
.fontSize(16)
.fontColor('#2979ff')
} else {
// 如果是Resource类型,显示图片作为图标
Image(this.item.icon)
.width(24)
.height(24)
.objectFit(ImageFit.Contain)
}
}
.width(40)
.height(40)
.justifyContent(FlexAlign.Center)
.alignItems(VerticalAlign.Center)
.margin({ right: 12 })
.borderRadius(4)
.backgroundColor('#f5f5f5')
}
// 中间内容区域
Column() {
// 标题
Text(this.item.title)
.fontSize(16)
.fontWeight(FontWeight.Medium)
.fontColor(this.item.disabled ? '#bdbdbd' : '#333')
.width('100%')
.textAlign(TextAlign.Start)
// 描述文本(如果有)
if (this.item.note) {
Text(this.item.note)
.fontSize(14)
.fontColor(this.item.disabled ? '#e0e0e0' : '#999')
.width('100%')
.textAlign(TextAlign.Start)
.margin({ top: 4 })
}
}
.layoutWeight(1)
.alignItems(HorizontalAlign.Start)
// 右侧区域
Row() {
// 右侧文本(如果有且允许显示)
if (this.showRightText && this.item.rightText) {
Text(this.item.rightText)
.fontSize(14)
.fontColor(this.item.disabled ? '#e0e0e0' : '#999')
.margin({ right: 4 })
}
// 右侧箭头(如果允许导航且显示箭头)
if (this.navigable && this.item.showArrow !== false) {
Image($r('app.media.ic_arrow_right'))
.width(16)
.height(16)
.objectFit(ImageFit.Contain)
.opacity(this.item.disabled ? 0.3 : 1)
}
}
.alignItems(VerticalAlign.Center)
}
.width('100%')
.padding({ left: 16, right: 16, top: 12, bottom: 12 })
.backgroundColor(this.item.disabled ? '#fafafa' : Color.White)
.borderRadius(8)
.onClick(() => {
// 如果项目未禁用且有点击回调,则执行回调
if (!this.item.disabled && this.item.onClick) {
this.item.onClick();
}
})
}
}
属性 | 类型 | 说明 |
---|---|---|
item | ListItemProps | 列表项数据,包含标题、描述、图标等 |
showThumbnail | boolean | 是否显示缩略图 |
showIcon | boolean | 是否显示图标 |
showRightText | boolean | 是否显示右侧文本 |
navigable | boolean | 是否可导航(控制右侧箭头显示) |
UVListItem组件的布局分为三个主要区域:
UVList组件使用条件渲染来优化性能和提高灵活性:
// 条件渲染标题
if (this.props.title) {
Text(this.props.title)
// 样式设置
}
// 条件渲染图标
if (this.showIcon && this.item.icon) {
// 渲染图标
}
// 条件渲染描述文本
if (this.item.note) {
Text(this.item.note)
// 样式设置
}
UVListItem组件支持两种类型的图标渲染:
// 根据图标类型显示不同的内容
if (typeof this.item.icon === 'string') {
// 如果是字符串类型,显示文本作为图标
Text(this.item.icon)
.fontSize(16)
.fontColor('#2979ff')
} else {
// 如果是Resource类型,显示图片作为图标
Image(this.item.icon)
.width(24)
.height(24)
.objectFit(ImageFit.Contain)
}
UVListItem组件根据列表项的状态(如禁用状态)调整样式:
// 标题文本颜色根据禁用状态调整
.fontColor(this.item.disabled ? '#bdbdbd' : '#333')
// 描述文本颜色根据禁用状态调整
.fontColor(this.item.disabled ? '#e0e0e0' : '#999')
// 背景颜色根据禁用状态调整
.backgroundColor(this.item.disabled ? '#fafafa' : Color.White)
// 箭头透明度根据禁用状态调整
.opacity(this.item.disabled ? 0.3 : 1)
UVList组件使用Column作为根容器,包含标题和列表内容:
Column() {
// 标题(如果有)
// 列表内容
}
.width('100%')
UVListItem组件使用Row作为根容器,包含左侧图标、中间内容和右侧区域:
Row() {
// 左侧图标区域
// 中间内容区域
// 右侧区域
}
.width('100%')
.padding({ left: 16, right: 16, top: 12, bottom: 12 })
.backgroundColor(this.item.disabled ? '#fafafa' : Color.White)
.borderRadius(8)
UVList组件提供了丰富的样式定制选项:
UVListItem组件通过onClick方法处理点击事件:
.onClick(() => {
// 如果项目未禁用且有点击回调,则执行回调
if (!this.item.disabled && this.item.onClick) {
this.item.onClick();
}
})
UVListItem组件通过检查item.disabled属性来处理禁用状态:
在下一篇教程中,我们将介绍UVList组件的使用方法和实际应用场景,敬请期待!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有