1、HarmonyOS grid里面的item支持拖动问题?
想要grid里面的item支持拖动,拖出来后可以删除,下面的代码就是你们上次给我提供的,正常情况下是可以使用的但是,往下拖的过程中遇到了TextInput后,gridItem的onDragMove就不会走了,我给TextInput设置了draggable(false)后无法解决
import { media } from '@kit.MediaKit';
import { BusinessError } from '@kit.BasicServicesKit';
import util from '@ohos.util';
import { curves } from '@kit.ArkUI';
@Entry
@Component
struct Index {
@State numbers: number[] = []
@State isTextInputVisible: boolean = true
@State isGridItemVisible: Visibility = Visibility.Visible
scroller: Scroller = new Scroller()
@State enableScroll: boolean = true
@State scaleItem: number = -1
@State dragItem: number = -1
@State offsetX: number = 0
@State offsetY: number = 0
@State currentY: number = 0
private dragRefOffsetx: number = 0
private dragRefOffsety: 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)
}
}
//向左滑
left(index: number): void {
this.offsetX += this.FIX_VP_X
this.dragRefOffsetx -= this.FIX_VP_X
this.itemMove(index, index - 1)
}
//向右滑
right(index: number): void {
this.offsetX -= this.FIX_VP_X
this.dragRefOffsetx += this.FIX_VP_X
this.itemMove(index, index + 1)
}
itemMove(index: number, newIndex: number): void {
console.info('index:' + index + ' newIndex:' + newIndex)
let tmp = this.numbers.splice(index, 1)
this.numbers.splice(newIndex, 0, tmp[0])
}
build() {
Column({ space: 5 }) {
Grid(this.scroller) {
ForEach(this.numbers, (item: number) => {
GridItem() {
Text(item + '')
.fontSize(16)
.width('100%')
.textAlign(TextAlign.Center)
.height(100)
.borderRadius(10)
.backgroundColor(0xFFFFFF)
.animation({ curve: Curve.Sharp, duration: 300 })
}
.visibility(this.dragItem == item ? Visibility.Hidden : Visibility.Visible) // 根据被拖动的item来设置visibility
.width(100)
.height(100)
.padding(5)
.zIndex(this.dragItem == item ? 1 : 0)
.scale({ x: this.scaleItem == item ? 1.05 : 1, y: this.scaleItem == item ? 1.05 : 1 })
.translate(this.dragItem == item ? { x: this.offsetX, y: this.offsetY } : { x: 0, y: 0 })
.onDragStart((event: DragEvent, extraParams?: string) => {
animateTo({ curve: Curve.Friction, duration: 300 }, () => {
this.scaleItem = item
})
this.dragItem = item
this.dragRefOffsetx = event.getWindowX()
this.dragRefOffsety = event.getWindowY()
})
.onDrop((event: DragEvent, extraParams?: string) => {
})
.onDragMove((event: DragEvent, extraParams?: string) => {
this.enableScroll = false
console.log('onDragMove GridItem getWindowX', event.getWindowX())
console.log('onDragMove GridItem getWindowY', event.getWindowY())
this.offsetX = event.getWindowX() - this.dragRefOffsetx
this.offsetY = event.getWindowY() - this.dragRefOffsety
this.currentY = event.getWindowY()
console.log('onDragMove GridItem offsetX', this.currentY)
animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
let index = this.numbers.indexOf(this.dragItem)
console.log('index', index)
if (this.offsetX >= this.FIX_VP_X / 2 && this.offsetY < this.FIX_VP_Y) {
this.right(index)
} else if (this.offsetX <= -this.FIX_VP_X / 2 && this.offsetY < this.FIX_VP_Y) {
this.left(index)
}
})
this.isGridItemVisible = Visibility.Hidden
})
.onDragEnd(() => {
this.enableScroll = true
this.isTextInputVisible = true
this.isGridItemVisible = Visibility.Visible
animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
this.dragItem = -1
this.scaleItem = -1
})
})
}, (item: number) => item.toString())
}
.padding({ left: 20, right: 20 })
.width('100%')
.height('120')
.scrollBar(BarState.Off)
.rowsTemplate('1fr')
.columnsGap(5)
.editMode(true)
.edgeEffect(EdgeEffect.Spring)
.backgroundColor('#0D182431')
.enabled(this.enableScroll)
// Modified TextInput to ignore drag events
TextInput({placeholder: '添加标题'})
.width('100%')
.height(200)
// .visibility(this.isTextInputVisible ? Visibility.Visible : Visibility.Hidden)
// .onDragMove(() => {
// // 隐藏TextInput
// this.isTextInputVisible = false
// })
Text('删除')
.width('100%')
.height(50)
.backgroundColor(Color.Red)
.position({ bottom: 0 })
.visibility(this.currentY > 580 ? Visibility.Visible : Visibility.Hidden)
.hitTestBehavior(HitTestMode.None)
}
.width('100%')
.height('100%')
.margin({ top: 5 })
.backgroundColor('#0D182431')
}
}
2、HarmonyOS Webview的onErrorReceive里回调的错误码,和文档里对不上?
Webview的onErrorReceive里回调的错误码,和文档里对不上:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/errorcode-webview-V5
有的返回-2,有的返回-106,有的-302。
参考以下文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-neterrorlist-V5
3、HarmonyOS 如何通过context: common.Context获取页面的名称和唯一性?
- 在当前页面可以使用router.getstat来获取页面名称:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-router-V5
- 使用getStringValue获取标识:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-resource-manager-V5
4、HarmonyOS 咨询文本动效、点击从某个位置显示Text并向上Y移动一定距离后自动隐藏;并在连续快速点击时,多个Text显示并根据点击的间隔时间,依次向上位置一段距离并隐藏,实现类似弹幕效果?
Text组件文本动效、点击从某个位置显示Text并向上Y移动一定距离后自动隐藏;并在连续快速点击时,多个Text显示并根据点击的间隔时间,依次向上位置一段距离并隐藏,实现类似弹幕效果。
参考下面的方法:
- 创建Image组件点击时文本出现
- 文本用@buildPrams,每点击一次出现文字
- 文本特效可以借助transition实现