该组件用来展示列表item分组,宽度默认充满List组件,必须配合List组件来使用。
private DataGroup:object[] = [
{
header:'静夜思',
content:['窗前明月光', '疑是地上霜', '举头望明月', '低头思故乡'],
footer:'唐 - 李白'
},
{
header:'鹿柴',
content:['空山不见人', '但问人语响', '返景入深林', '复照青苔上'],
footer:'唐 - 王维'
}
]
@Entry
@Component
struct ListItemGroupNote {
private DataGroup: object[] = [
{
header: '静夜思',
content: ['窗前明月光', '疑是地上霜', '举头望明月', '低头思故乡'],
footer: '唐 - 李白'
},
{
header: '鹿柴',
content: ['空山不见人', '但问人语响', '返景入深林', '复照青苔上'],
footer: '唐 - 王维'
}
]
@Builder headerItem(title: string) {
}
@Builder footerItem(title: string) {
}
build() {
Row() {
Column() {
List() {
ForEach(this.DataGroup, (item) => {
ListItemGroup({
header:this.headerItem(item.header),
footer:this.footerItem(item.footer)
}) {
ForEach(item.content, (itemA)=>{
ListItem(){
Text(itemA)
.width('100%').height(40).fontSize(25)
}
})
}
})
}
}
.width('100%')
.height('100%')
}
.width('100%')
.height('100%')
}
}
**接下来我们只需要 将 自定义组件内的样式完善即可 , 如下 **
@Builder headerItem(title: string) {
Text(title).fontSize(25).height(50).width('100%').backgroundColor('#e5e5e5')
}
@Builder footerItem(title: string) {
Text(title).fontSize(25).height(50).width('100%').backgroundColor('#e5e5e5')
}
我们在List 组件和ListItemGroup 中添加 space 属性使其样式更加完美一些 , 效果如下
divider({
strokeWidth:3,
color:'#e4e4e4',
startMargin:10,
endMargin:10
})
效果如图
@Entry
@Component
struct ListItemGroupNote {
private DataGroup: object[] = [
{
header: '静夜思',
content: ['窗前明月光', '疑是地上霜', '举头望明月', '低头思故乡'],
footer: '唐 - 李白'
},
{
header: '鹿柴',
content: ['空山不见人', '但问人语响', '返景入深林', '复照青苔上'],
footer: '唐 - 王维'
}
]
@Builder headerItem(title: string) {
Text(title).fontSize(25).height(50).width('100%').backgroundColor('#e5e5e5')
}
@Builder footerItem(title: string) {
Text(title).fontSize(25).height(50).width('100%').backgroundColor('#e5e5e5')
}
build() {
Row() {
Column() {
List({space:20}) {
ForEach(this.DataGroup, (item) => {
ListItemGroup({
header:this.headerItem(item.header),
footer:this.footerItem(item.footer),
space:10
}) {
ForEach(item.content, (itemA)=>{
ListItem(){
Text(itemA)
.width('100%').height(40).fontSize(25)
}
})
}.divider({
strokeWidth:3,
color:'#e4e4e4',
startMargin:10,
endMargin:10
})
})
}
}
.width('100%')
.height('100%')
}
.width('100%')
.height('100%')
}
}