【每日学点鸿蒙知识】线程创建、构造函数中创建变量仍报错、List上下拖拽,调用JS代码、无法选择本地csr文件问题
1、HarmonyOS 如何创建一个单线程?
- 请问 worker 是单线程还是多线程?
- 如果 worker 不是单线程,如何创建一个单线程呢?
ArkTS是单线程模型,所以worker也是单线程,他是在宿主线程上创建的一个子线程:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/worker-introduction-V5
2、HarmonyOS 已经在构造函数中创建了变量,还是提示错误?
![[【每日学点鸿蒙知识】24.10.29.png]]
参考代码:
cat1?:Cat
constructor() {
super();
this.cat1 = new Cat()
}
//或者:
cat1:Cat = new Cat()
constructor() {
super();
//this.cat1 = new Cat()
}
//或者:
cat1!:Cat
constructor() {
super();
this.cat1 = new Cat()
}
3、HarmonyOS List列表如何支持item的上下拖拽排序?
参考以下demo:
import curves from '@ohos.curves';
import Curves from '@ohos.curves' // xxx.ets
@Entry
@Component
struct ListDemo {
@State private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@State dragItem: number = -1
@State scaleItem: number = -1
@State neighborItem: number = -1
@State neighborScale: number = -1
private dragRefOffset: number = 0
@State offsetX: number = 0
@State offsetY: number = 0
private ITEM_INTV: number = 120
scaleSelect(item: number): number {
if (this.scaleItem == item) {
return 1.05
}
else if (this.neighborItem == item) {
return this.neighborScale
} else {
return 1
}
}
itemMove(index: number, newIndex: number): void {
let tmp = this.arr.splice(index, 1)
this.arr.splice(newIndex, 0, tmp[0])
}
build() {
Stack() {
List({ space: 20, initialIndex: 0 }) {
ForEach(this.arr, (item: number) => {
ListItem() {
Text('' + item)
.width('100%')
.height(100)
.fontSize(16)
.textAlign(TextAlign.Center)
.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 })
}
.margin({ left: 12, right: 12 })
.scale({ x: this.scaleSelect(item), y: this.scaleSelect(item) })
.zIndex(this.dragItem == item ? 1 : 0)
.translate(this.dragItem == item ? { y: this.offsetY } : { y: 0 })
.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.dragRefOffset = 0
})
.onActionUpdate((event: GestureEvent) => {
this.offsetY = event.offsetY - this.dragRefOffset
// console.log('Y:' + this.offsetY.toString())
this.neighborItem = -1
let index = this.arr.indexOf(item)
let curveValue = Curves.initCurve(Curve.Sharp)
let value: number = 0 //根据位移计算相邻项的缩放
if (this.offsetY < 0) {
value = curveValue.interpolate(-this.offsetY / this.ITEM_INTV)
this.neighborItem = this.arr[index-1]
this.neighborScale = 1 - value / 20;
console.log('neighborScale:' + this.neighborScale.toString())
} else if (this.offsetY > 0) {
value = curveValue.interpolate(this.offsetY / this.ITEM_INTV)
this.neighborItem = this.arr[index+1]
this.neighborScale = 1 - value / 20;
} //根据位移交换排序
if (this.offsetY > this.ITEM_INTV / 2) {
animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
this.offsetY -= this.ITEM_INTV
this.dragRefOffset += this.ITEM_INTV
this.itemMove(index, index + 1)
})
}
else if (
this.offsetY < -this.ITEM_INTV / 2) {
animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
this.offsetY += this.ITEM_INTV
this.dragRefOffset -= this.ITEM_INTV
this.itemMove(index, index - 1)
})
}
})
.onActionEnd((event: GestureEvent) => {
animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
this.dragItem = -1
this.neighborItem = -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
this.neighborItem = -1
})
animateTo({ curve: curves.interpolatingSpring(14, 1, 170, 17), delay: 150 }, () => {
this.scaleItem = -1
})
}))
}, (item: number) => item.toString())
}
}
.width('100%')
.height('100%')
.backgroundColor(0xDCDCDC)
.padding({ top: 5 })
}
}
4、HarmonyOS 如何调用js代码?
应用侧可以通过runJavaScript()方法调用前端页面的JavaScript相关函数,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/web-in-app-frontend-page-function-invoking-V5
5、HarmonyOS 创建开发和发布证书,无法选择本地csr文件?
请尝试以下方案
- 检查一下账号是否已实名认证;
- 每个帐号最多申请1个发布证书,每个帐号最多申请2个调试证书;
- 如果证书已过期或者不再使用,点击废除即可删除证书;删除过期证书刷新页面后确认一下是否可新增证书;
参考文档链接:https://developer.huawei.com/consumer/cn/doc/app/agc-help-releaseharmony-0000001933963166