用户3116712
用户3116712
  • 发布:2026-09-08 12:00
  • 更新:2026-09-08 12:00
  • 阅读:9

如何实现拖拽时列表项占位动画的效果

分类:HBuilder

拖拽Grid时,列表项显示占位动画效果。实现步骤如下:

在Grid组件下设置属性editMode(true),使Grid进入编辑模式。进入编辑模式后,可以拖拽Grid组件内部的GridItem。
在onItemDragStart回调中设置拖拽时显示的组件。
在onItemDrop中获取拖拽起始位置和拖拽插入位置,并完成数组位置交换逻辑。
@Entry
@Component
struct GridExample {
@State numbers: string[] = [];
scroller: Scroller = new Scroller();
@State text: string = 'drag';

@Builder
pixelMapBuilder() {
Column() {
Text(this.text)
.fontSize(16)
.backgroundColor(0xF9CF93)
.width(80)
.height(80)
.textAlign(TextAlign.Center)
}
}

aboutToAppear() {
for (let i = 1; i <= 15; i++) {
this.numbers.push(i + '');
}
}

changeIndex(index1: number, index2: number) {
// Swap array positions
let temp = this.numbers[index1];
this.numbers[index1] = this.numbers[index2];
this.numbers[index2] = temp;
}

build() {
Column({ space: 5 }) {
Grid(this.scroller) {
ForEach(this.numbers, (day: string) => {
GridItem() {
Text(day)
.fontSize(16)
.backgroundColor(0xF9CF93)
.width(80)
.height(80)
.textAlign(TextAlign.Center)
.onTouch((event: TouchEvent) => {
if (event.type === TouchType.Up) {
this.text = day;
}
})
}
})
}
.columnsTemplate('1fr 1fr 1fr')
.columnsGap(10)
.rowsGap(10)
.onScrollIndex((first: number) => {
console.info(first.toString());
})
.width('90%')
.backgroundColor(0xFAEEE0)
.height(300)
.editMode(true) // Set whether the Grid enters editing mode. When entering editing mode, you can drag and drop the GridItem inside the Grid component
.onItemDragStart((event: ItemDragInfo, itemIndex: number) => { // When dragging the component bound to this event for the first time, a callback is triggered.
return this.pixelMapBuilder(); //Set the image displayed during the drag and drop process.
})
// The component bound to this event can be used as a drag and drop release target. When the drag behavior stops within the scope of this component, a callback is triggered.
.onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => {
// Drag the starting position of itemIndex, drag the insertion position of insertIndex
this.changeIndex(itemIndex, insertIndex)
})
}.width('100%').margin({ top: 5 })
}
}
https://sites.google.com/view/7wtipu12/home
https://sites.google.com/view/4duref63/home
https://sites.google.com/view/7wvqsg34/home
https://sites.google.com/view/0xxrqf74/home
https://sites.google.com/view/1ruukx88/home
https://sites.google.com/view/1byjvn60/home
https://sites.google.com/view/4xgvng21/home
https://sites.google.com/view/9pciml46/home
https://sites.google.com/view/2hbbnf70/home
https://sites.google.com/view/9rghya88/home
https://sites.google.com/view/7vwdre23/home
https://sites.google.com/view/7przvh02/home
https://sites.google.com/view/3hktye35/home
https://sites.google.com/view/0xykfj78/home
https://sites.google.com/view/0asapr45/home
https://sites.google.com/view/0rgboz72/home
https://sites.google.com/view/0qrzbw79/home
https://sites.google.com/view/4pszed21/home
https://sites.google.com/view/5rjciu59/home
https://sites.google.com/view/0ssfsw28/home
https://sites.google.com/view/7pmhty48/home
https://sites.google.com/view/2dceln83/home
https://sites.google.com/view/1mmlgm03/home
https://sites.google.com/view/1tpbqp74/home
https://sites.google.com/view/9sfedj48/home
https://sites.google.com/view/3ibsri54/home
https://sites.google.com/view/6kncht37/home
https://sites.google.com/view/1aczwe01/home
https://sites.google.com/view/3sxmeg54/home
https://sites.google.com/view/7eprfq47/home
https://sites.google.com/view/1ebnwt11/home
https://sites.google.com/view/5cywba48/home
https://sites.google.com/view/8vqwer81/home
https://sites.google.com/view/8wetjp74/home
https://sites.google.com/view/8goxua35/home
https://sites.google.com/view/4kslyk33/home
https://sites.google.com/view/4essyn94/home
https://sites.google.com/view/2jvyst79/home
https://sites.google.com/view/1roras95/home
https://sites.google.com/view/1ktawz57/home
https://sites.google.com/view/0rgcul98/home
https://sites.google.com/view/0nycaz65/home
https://sites.google.com/view/1omztt55/home
https://sites.google.com/view/7gdqsw63/home
https://sites.google.com/view/4pxdgz83/home
https://sites.google.com/view/0gwyrr33/home
https://sites.google.com/view/2ijimf20/home
https://sites.google.com/view/8wykzr07/home
https://sites.google.com/view/7nudnf27/home
https://sites.google.com/view/3kcowk62/home
https://sites.google.com/view/5pytzw49/home
https://sites.google.com/view/2slklr71/home
https://sites.google.com/view/2ikmgs30/home
https://sites.google.com/view/5hqxfe84/home
https://sites.google.com/view/0kqdmc12/home
https://sites.google.com/view/5axjec74/home
https://sites.google.com/view/5gixkd52/home
https://sites.google.com/view/6kgtbh91/home
https://sites.google.com/view/1dtvwv05/home
https://sites.google.com/view/3nbvob04/home
https://sites.google.com/view/2fpwug45/home
https://sites.google.com/view/6xjygc54/home
https://sites.google.com/view/4uqced58/home
https://sites.google.com/view/5whcuq51/home
https://sites.google.com/view/2rgfcv68/home
https://sites.google.com/view/5pwouh86/home
https://sites.google.com/view/6dwmpc30/home
https://sites.google.com/view/3wihcp64/home
https://sites.google.com/view/2huocs89/home
https://sites.google.com/view/3mmiei39/home
https://sites.google.com/view/1rvjfn27/home
https://sites.google.com/view/6lcinp92/home
https://sites.google.com/view/6gxkse35/home
https://sites.google.com/view/3wahfy11/home
https://coub.com/view/5rqufxinzg
https://coub.com/view/ropziici0u
https://coub.com/view/7or1trdg1c
https://coub.com/view/vcozxsnzer
https://coub.com/view/7kd7t3oj2c
https://coub.com/view/8aqyygye2e
https://coub.com/view/gllow4joor
https://coub.com/view/39dnitz99r
https://coub.com/view/1t0lqxrzi8
https://coub.com/view/vnj2yxw549
https://coub.com/view/3s1pmq3ilx
https://coub.com/view/a1ytt3pw3a
https://coub.com/view/cawk3s04k4
https://coub.com/view/kmvzuhkhjd
https://coub.com/view/ej9o0adzwx
https://coub.com/view/octx2ubxfv
https://coub.com/view/aa9gmfdu3q
https://coub.com/view/9bgaznk1ck
https://coub.com/view/rchkbwofuc
https://coub.com/view/9s2jppcl9u
https://coub.com/view/5sj5dui5qx
https://coub.com/view/m2ajgr2j7k
https://coub.com/view/0yfa17r86g
https://coub.com/view/gr0kpo0pmh
https://coub.com/view/l3hqw0zqc5
https://coub.com/view/88qknf3fk6
https://coub.com/view/isb8wfeda2
https://coub.com/view/yvsr9c6x01
https://coub.com/view/mb687s5mws
https://coub.com/view/rgb9qqxyvj
https://coub.com/view/03xbg531k6
https://coub.com/view/0p29ulxra3
https://coub.com/view/8815w7t7de
https://coub.com/view/shicmahfum
https://coub.com/view/gpbeot9j3l
https://coub.com/view/2dfpjx3czu
https://coub.com/view/xs3ukwnjf3
https://coub.com/view/g71qbn6jru
https://coub.com/view/nshhnvyttc
https://coub.com/view/jmz1bnvuks
https://coub.com/view/fc80zsrnh1
https://coub.com/view/ppu1e0wlob
https://coub.com/view/o8b8ik13xp
https://coub.com/view/wjc31q3bpo
https://coub.com/view/s3h67xw7jb
https://coub.com/view/zofzxw59k4
https://coub.com/view/kdi3wrrueu
https://coub.com/view/98nrx1y16i
https://coub.com/view/agp7euis6f
https://coub.com/view/alaeuzimeo
https://coub.com/view/6y73g5cr1s
https://coub.com/view/vfnl3o6o5c
https://coub.com/view/de4e7iqa60
https://coub.com/view/c4htjqg88m
https://coub.com/view/kz3ieaosfi
https://coub.com/view/d6msuxbq4a
https://coub.com/view/77tzitriot
https://coub.com/view/lpsk81odp9
https://coub.com/view/2nmdnujl00
https://coub.com/view/l8mq0g4760
https://coub.com/view/2mkc8ze5zx
https://coub.com/view/ig0wfzltc6
https://coub.com/view/1ycu3zf1lh
https://coub.com/view/wzat8knm2z
https://coub.com/view/64fv7jr83g
https://coub.com/view/a3did3vro0
https://coub.com/view/ti645azepy
https://coub.com/view/g1vuwgj26b
https://coub.com/view/ffzr61ask8
https://coub.com/view/p5bbm0hek0
https://coub.com/view/g3cd11h0y6
https://coub.com/view/3sf0ida4ix
https://coub.com/view/7bu9ihff89
https://coub.com/view/xjn62txhsl
https://coub.com/view/nlk1uu6rts
https://coub.com/view/t0d2e48ya0
https://coub.com/view/nseh2ehyd9
https://coub.com/view/5v4pyo0v0k
https://coub.com/view/rierf8fjyo

0 关注 分享

要回复文章请先登录注册