9 Star 0 Fork 0

yanshuifeng/repeat_feature_demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
grid_layout_options_with_repeat.ets 3.31 KB
一键复制 编辑 原始数据 按行查看 历史
xxx 提交于 2024-06-16 14:34 . add grid test with layout options
@ObservedV2
class GridArrayHolder {
@Trace arr: Array<number> = [];
constructor(count: number) {
for (let i = 0; i < count; ++i) {
this.arr.push(i)
}
}
}
//@Reusable
@ComponentV2
struct ReusableItem {
@Param reusableItem: number = 0
aboutToAppear(): void {
console.info('item:' + this.reusableItem)
}
build() {
Column() {
Button('N' + this.reusableItem).height(50)
Image('res/picture(' + this.reusableItem % 5 + ').jpg')
.objectFit(ImageFit.Fill)
.width('100%')
.height(100)
}
}
}
@Entry
@ComponentV2
struct ParentGrid {
@Local arrayHolder: GridArrayHolder = new GridArrayHolder(20);
@Local totalCount = this.arrayHolder.arr.length
@Local options: GridLayoutOptions = { regularSize: [1, 1], irregularIndexes: [this.totalCount] }
scroller: Scroller = new Scroller();
build() {
Column({ space: 5 }) {
Text('grid_repeat_test.ets')
Row() {
Button('scrollTo 0').height(100).onClick(() => {
this.scroller.scrollToIndex(0)
})
Button('scrollTo 4').height(100).onClick(() => {
this.scroller.scrollToIndex(4)
})
Button('scrollTo 10').height(100).onClick(() => {
this.scroller.scrollToIndex(10)
})
}
Row() {
Button(`change[0] ${this.arrayHolder.arr[0]}`).height(100).onClick(() => {
this.arrayHolder.arr[0] = this.arrayHolder.arr[0] + this.arrayHolder.arr[0]
})
Button('delete[0]').height(100).onClick(() => {
this.arrayHolder.arr.shift()
this.totalCount = this.arrayHolder.arr.length
this.options.irregularIndexes = [this.totalCount]
})
Button('add 10').height(100).onClick(() => {
let oldCount = this.arrayHolder.arr.length
let newData: number[] = []
for (let i = 0; i < 10; ++i) {
newData.push(oldCount + i)
}
this.arrayHolder.arr.splice(oldCount, 0, ...newData)
this.totalCount = this.arrayHolder.arr.length
this.options.irregularIndexes = [this.totalCount]
})
}
Grid(this.scroller, this.options) {
Repeat(this.arrayHolder.arr)
.virtualScroll({ totalCount: this.totalCount })
.key((item, index) => `${index}-${item}`)
.each((r) => {
GridItem() {
ReusableItem({ reusableItem: r.item })
}.backgroundColor(Color.Green)
})
GridItem() {
Text('last Item').width('100%').height(50).fontColor(Color.White).backgroundColor(Color.Blue)
}
}
.onScrollIndex((start, end) => {
console.log('onScrollIndex', start, end);
})
.onReachEnd(() => {
let oldCount = this.arrayHolder.arr.length
let newData: number[] = []
for (let i = 0; i < 10; ++i) {
newData.push(oldCount + i)
}
this.arrayHolder.arr.push(...newData)
this.totalCount = this.arrayHolder.arr.length
this.options.irregularIndexes = [this.totalCount]
})
.columnsTemplate('1fr 1fr')
.columnsGap(10)
.rowsGap(10)
.width('90%')
.backgroundColor(0xFAEEE0)
.height(300)
Button('next page')
.onClick(() => {
this.scroller.scrollPage({ next: true })
})
}.width('100%').margin({ top: 5 })
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yan-shuifeng/repeat_feature_demo.git
git@gitee.com:yan-shuifeng/repeat_feature_demo.git
yan-shuifeng
repeat_feature_demo
repeat_feature_demo
master

搜索帮助