HarmonyOS 鸿蒙Next如何做一个可以拖动随意配置的菜单
HarmonyOS 鸿蒙Next如何做一个可以拖动随意配置的菜单
就是APP首页实现一个动态配置的菜单,可以让用户随意配置
目前使用Grid布局显示,现在要实现这个菜单可以动态拖动顺序,又可以删除和添加。
目前使用Grid布局显示,现在要实现这个菜单可以动态拖动顺序,又可以删除和添加。
2 回复
可以使用onItemDragStart(event: (event: ItemDragInfo, itemIndex: number) => (() => any) | void)开始拖拽网格元素时触发事件(删除)
也可以onItemDragEnter(event: (event: ItemDragInfo) => void)拖拽进入网格元素范围内时触发。(删除)
及onItemDragMove(event: (event: ItemDragInfo, itemIndex: number, insertIndex: number) => void)拖拽在网格元素范围内移动时触发(删除)
等多种选择,详细请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-grid-V5#%E4%BA%8B%E4%BB%B6
拖拽参考以下demo:
// 固定前两个GridItem不参与拖拽排序的功能
import curves from '@ohos.curves'
// xxx.ets
[@Entry](/user/Entry)
[@Component](/user/Component)
struct GridItemExample {
[@State](/user/State) numbers: number[] = []
[@State](/user/State) dragItem: number = -1
[@State](/user/State) scaleItem: number = -1
[@State](/user/State) item: number = -1
private dragRefOffsetx: number = 0
private dragRefOffsety: number = 0
[@State](/user/State) offsetX: number = 0
[@State](/user/State) offsetY: number = 0
private FIX_VP_X: number = 108
private FIX_VP_Y: number = 120
aboutToAppear() {
for (let i = 1; i <= 11; i++) {
this.numbers.push(i)
}
}
itemMove(index: number, newIndex: number): void {
console.info('index:' + index + ' newIndex:' + newIndex)
if (!this.isDraggable(newIndex)) {
return
}
let tmp = this.numbers.splice(index, 1)
this.numbers.splice(newIndex, 0, tmp[0])
}
//向下滑
down(index: number): void {
// 指定固定GridItem不响应事件
if (!this.isDraggable(index + 3)) {
return
}
this.offsetY -= this.FIX_VP_Y
this.dragRefOffsety += this.FIX_VP_Y
this.itemMove(index, index + 3)
}
//向下滑(右下角为空)
down2(index: number): void {
if (!this.isDraggable(index + 3)) {
return
}
this.offsetY -= this.FIX_VP_Y
this.dragRefOffsety += this.FIX_VP_Y
this.itemMove(index, index + 3)
}
//向上滑
up(index: number): void {
if (!this.isDraggable(index - 3)) {
return
}
this.offsetY += this.FIX_VP_Y
this.dragRefOffsety -= this.FIX_VP_Y
this.itemMove(index, index - 3)
}
//向左滑
left(index: number): void {
if (!this.isDraggable(index - 1)) {
return
}
this.offsetX += this.FIX_VP_X
this.dragRefOffsetx -= this.FIX_VP_X
this.itemMove(index, index - 1)
}
//向右滑
right(index: number): void {
if (!this.isDraggable(index + 1)) {
return
}
this.offsetX -= this.FIX_VP_X
this.dragRefOffsetx += this.FIX_VP_X
this.itemMove(index, index + 1)
}
//向右下滑
lowerRight(index: number): void {
if (!this.isDraggable(index + 4)) {
return
}
this.offsetX -= this.FIX_VP_X
this.dragRefOffsetx += this.FIX_VP_X
this.offsetY -= this.FIX_VP_Y
this.dragRefOffsety += this.FIX_VP_Y
this.itemMove(index, index + 4)
}
//向右上滑
upperRight(index: number): void {
if (!this.isDraggable(index - 2)) {
return
}
this.offsetX -= this.FIX_VP_X
this.dragRefOffsetx += this.FIX_VP_X
this.offsetY += this.FIX_VP_Y
this.dragRefOffsety -= this.FIX_VP_Y
this.itemMove(index, index - 2)
}
//向左下滑
lowerLeft(index: number): void {
if (!this.isDraggable(index + 2)) {
return
}
this.offsetX += this.FIX_VP_X
this.dragRefOffsetx -= this.FIX_VP_X
this.offsetY -= this.FIX_VP_Y
this.dragRefOffsety += this.FIX_VP_Y
this.itemMove(index, index + 2)
}
//向左上滑
upperLeft(index: number): void {
if (!this.isDraggable(index - 4)) {
return
}
this.offsetX += this.FIX_VP_X
this.dragRefOffsetx -= this.FIX_VP_X
this.offsetY += this.FIX_VP_Y
this.dragRefOffsety -= this.FIX_VP_Y
this.itemMove(index, index - 4)
}
isDraggable(index: number): boolean {
console.info('index:' + index)
return index > 1
}
build() {
Column() {
Grid() {
ForEach(this.numbers, (item: number) => {
GridItem() {
Text(item + '')
.fontSize(16)
.width('100%')
.textAlign(TextAlign.Center)
.height(100)
.borderRadius(10)
.backgroundColor(0xFFFFFF)
.shadow(this.scaleItem == item ? {
radius: 70,
color: '#15000000',
offsetX: 0,
offsetY: 0
} :
{
radius: 0,
color: '#15000000',
offsetX: 0,
offsetY: 0
})
.animation({ curve: Curve.Sharp, duration: 300 })
}
// 指定固定GridItem不响应事件
.hitTestBehavior(this.isDraggable(this.numbers.indexOf(item)) ? HitTestMode.Default : HitTestMode.None)
.scale({ x: this.scaleItem == item ? 1.05 : 1, y: this.scaleItem == item ? 1.05 : 1 })
.zIndex(this.dragItem == item ? 1 : 0)
.translate(this.dragItem == item ? { x: this.offsetX, y: this.offsetY } : { x: 0, y: 0 })
.padding(10)
.gesture(
// 以下组合手势为顺序识别,当长按手势事件未正常触发时则不会触发拖动手势事件
GestureGroup(GestureMode.Sequence,
LongPressGesture({ repeat: true })
.onAction((event?: GestureEvent) => {
animateTo({ curve: Curve.Friction, duration: 300 }, () => {
this.scaleItem = item
})
})
.onActionEnd(() => {
animateTo({ curve: Curve.Friction, duration: 300 }, () => {
this.scaleItem = -1
})
}),
PanGesture({ fingers: 1, direction: null, distance: 0 })
.onActionStart(() => {
this.dragItem = item
this.dragRefOffsetx = 0
this.dragRefOffsety = 0
})
.onActionUpdate((event: GestureEvent) => {
this.offsetY = event.offsetY - this.dragRefOffsety
this.offsetX = event.offsetX - this.dragRefOffsetx
// console.log('X:' + this.offsetX.toString())
// console.log('Y:' + this.offsetY.toString())
animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
let index = this.numbers.indexOf(this.dragItem)
if (this.offsetY >= this.FIX_VP_Y / 2 && (this.offsetX <= 44 && this.offsetX >= -44)
&& ![8, 9, 10].includes(index)) {
//向下滑
this.down(index)
} else if (this.offsetY <= -this.FIX_VP_Y / 2 && (this.offsetX <= 44 && this.offsetX >= -44)
&& ![0, 1, 2].includes(index)) {
//向上滑
this.up(index)
} else if (this.offsetX >= this.FIX_VP_X / 2 && (this.offsetY <= 50 && this.offsetY >= -50)
&& ![2, 5, 8, 10].includes(index)) {
//向右滑
this.right(index)
} else if (this.offsetX <= -this.FIX_VP_X / 2 && (this.offsetY <= 50 && this.offsetY >= -50)
&& ![0, 3, 6, 9].includes(index)) {
//向左滑
this.left(index)
} else if (this.offsetX >= this.FIX_VP_X / 2 && this.offsetY >= this.FIX_VP_Y / 2
&& ![2, 5, 7, 8, 9, 10].includes(index)) {
//向右下滑
this.lowerRight(index)
} else if (this.offsetX >= this.FIX_VP_X / 2 && this.offsetY <= -this.FIX_VP_Y / 2
&& ![0, 1, 2, 5, 8].includes(index)) {
//向右上滑
this.upperRight(index)
} else if (this.offsetX <= -this.FIX_VP_X / 2 && this.offsetY >= this.FIX_VP_Y / 2
&& ![0, 3, 6, 9, 10].includes(index)) {
//向左下滑
this.lowerLeft(index)
} else if (this.offsetX <= -this.FIX_VP_X / 2 && this.offsetY <= -this.FIX_VP_Y / 2
&& ![0, 1, 2, 3, 6, 9].includes(index)) {
//向左上滑
this.upperLeft(index)
} else if (this.offsetX >= this.FIX_VP_X / 2 && this.offsetY >= this.FIX_VP_Y / 2
&& [7].includes(index)) {
//向右下滑(右下角为空)
this.down2(index)
}
})
})
.onActionEnd(() => {
animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
this.dragItem = -1
})
animateTo({
curve: curves.interpolatingSpring(14, 1, 170, 17), delay: 150
}, () => {
this.scaleItem = -1
})
})
)
.onCancel(() => {
animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
this.dragItem = -1
})
animateTo({
curve: curves.interpolatingSpring(14, 1, 170, 17)
}, () => {
this.scaleItem = -1
})
})
)
}, (item: number) => item.toString())
}
.width('90%')
.editMode(true)
.scrollBar(BarState.Off)
.columnsTemplate("1fr 1fr 1fr")
}.width('100%').height('100%').backgroundColor('#0D182431').padding({ top: 5 })
}
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
更多关于HarmonyOS 鸿蒙Next如何做一个可以拖动随意配置的菜单的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html