HarmonyOS鸿蒙Next中如何写一个竖向排列的List,嵌套Group,且分组里的item是横向排列的视图
HarmonyOS鸿蒙Next中如何写一个竖向排列的List,嵌套Group,且分组里的item是横向排列的视图呢?
如果必须使用Group,是实现不了的,文档中明确说明,ListItemGroup使用direction属性设置布局方向不生效,ListItemGroup组件布局方向跟随父容器List组件的布局方向。
可以在ListItem中嵌套Column-Row排版自定义实现,参考示例:
import { router } from '@kit.ArkUI';
@Entry
@Component
struct Index {
private timeTable: TimeTable[] = [
{
title: '星期一',
projects: ['语文', '数学', '英语']
},
{
title: '星期二',
projects: ['物理', '化学', '生物']
},
{
title: '星期三',
projects: ['历史', '地理', '政治']
},
{
title: '星期四',
projects: ['美术', '音乐', '体育']
}
];
@Builder
itemHead(text: string) {
Text(text)
.fontSize(20)
.backgroundColor(0xAABBCC)
.width('100%')
.padding(10)
}
@Builder
itemFoot(num: number) {
Text('共' + num + '节课')
.fontSize(16)
.backgroundColor(0xAABBCC)
.width('100%')
.padding(5)
}
build() {
Column() {
List({ space: 20 }) {
ForEach(this.timeTable, (item: TimeTable) => {
ListItem() {
Column() {
this.itemHead(item.title)
Row() {
ForEach(item.projects, (project: string) => {
Text(project)
.width('25%')
.height(100)
.fontSize(20)
.textAlign(TextAlign.Center)
.backgroundColor(0xFFFFFF)
}, (item: string) => item)
}
.justifyContent(FlexAlign.SpaceAround)
.width('100%')
this.itemFoot(item.projects.length)
}
}
})
}
.width('90%')
.sticky(StickyStyle.Header | StickyStyle.Footer)
.scrollBar(BarState.Off)
}.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
}
}
interface TimeTable {
title: string;
projects: string[];
}
更多关于HarmonyOS鸿蒙Next中如何写一个竖向排列的List,嵌套Group,且分组里的item是横向排列的视图的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
@Entry @Component struct ListExample { @State arr1: number[] = [0,1,2,3,4,5] @State arr2: number[] = [0,1,2,3,4,5]
build() { Column() { List({ space: ‘4vp’, initialIndex: 0 }) { ForEach(this.arr1, () => { ListItem() { Flex(){ ForEach(this.arr2, (item: number)=>{ Text(’’ + item) .width(‘100%’) .textAlign(TextAlign.Center) .backgroundColor(0xDCDCDC) }) }
}
})
}
.width('100%')
}
.width('100%')
.padding({ top: 5 })
} }
嗷,嵌套个flex就好了
在HarmonyOS鸿蒙Next中,可以使用ListContainer
和DirectionalLayout
实现竖向排列的List,嵌套Group,且分组里的item横向排列。首先,创建ListContainer
作为主容器,设置其方向为垂直。然后,在ListContainer
的ItemProvider
中,为每个Group创建一个DirectionalLayout
,设置其方向为水平,用于横向排列item。最后,将每个item添加到对应的DirectionalLayout
中。