HarmonyOS 鸿蒙Next FrameNode的点击事件如何透传给他的父控件
HarmonyOS 鸿蒙Next FrameNode的点击事件如何透传给他的父控件
FrameNode被点击后,如何将点击事件继续透传给他的父控件。让他的父控件知道自己的内部被点击了。
场景大概是这样,我们有一个特殊的自定义原生组件。可以通过传递进来的脚本去动态生成视图。现在要做一个点击事件反馈的功能。但是由于功能设定的缘故,要求该被点击组件以及其父控件的点击事件都被触发。在android中,同样的功能,我只需要这么做,view.getParent().callOnClick(),即可完成。但是在鸿蒙上没有callOnClick方法,导致该功能没找到实现方案。以下是视图创建的demo:
private createView(holder: NativePluginHolder): FrameNode | null {
if (this.uiContext && holder) {
const view = new FrameNode(this.uiContext);
view.commonAttribute
.width(holder.width+'px')
.height(holder.height+'px')
.margin({left: holder.x+'px', top: holder.y+'px'})
.backgroundColor(holder.backgroundColor)
.customProperty("holder",holder);
if (holder.type === NATIVE_PLUGIN_TYPE.COVER_VIEW) {
//由于view类型是可以添加子视图的,因此不得不多创建一个可以加子视图的视图
let stack = typeNode.createNode(this.uiContext, 'Stack');
stack.initialize({alignContent: Alignment.TopStart})
.onClick(()=>{
GmuLogProxy.debug(tag, "stack["+holder.ref+"] on click!");
})
if (holder.text) {
let text = typeNode.createNode(this.uiContext, 'Text');
text.initialize(holder.text)
.width('100%')
.height('100%')
.alignSelf(ItemAlign.Baseline)
.align(Alignment.TopStart)
.fontSize(holder.fontSize)
.fontWeight(holder.fontWeight)
.fontColor(holder.color)
.maxLines(1)
stack.appendChild(text);
}
view.appendChild(stack);
} else if (holder.type === NATIVE_PLUGIN_TYPE.COVER_IMAGE) {
let image = typeNode.createNode(this.uiContext, 'Image');
image.initialize(holder.src)
.width('100%')
.height('100%')
view.appendChild(image);
} else {
let stack = typeNode.createNode(this.uiContext, 'Stack');
stack.initialize({alignContent: Alignment.TopStart})
.onClick(()=>{
GmuLogProxy.debug(tag, "stack["+holder.ref+"] on click!");
})
view.appendChild(stack);
}
this.nodeMap.set(holder.ref, view);
return view;
}
return null;
}
更多关于HarmonyOS 鸿蒙Next FrameNode的点击事件如何透传给他的父控件的实战教程也可以访问 https://www.itying.com/category-93-b0.html
1 回复