From 3250523fb4fa5ecb2f8df278ae4bd1891a7240b9 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Fri, 8 Oct 2021 18:36:49 +0800 Subject: [PATCH] fix when remove child node from group node,reuse cause problem --- .../main/java/pub/doric/shader/GroupNode.java | 9 ++++----- doric-demo/src/Counter.ts | 19 ++++++++++++++++++- doric-iOS/Pod/Classes/Shader/DoricGroupNode.m | 14 ++------------ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/shader/GroupNode.java b/doric-android/doric/src/main/java/pub/doric/shader/GroupNode.java index 2a899570..ab7ee7e8 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/GroupNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/GroupNode.java @@ -108,15 +108,14 @@ public abstract class GroupNode extends SuperNode { } if (position >= 0) { //Found swap idx,position - ViewNode reused = mChildNodes.remove(position); - ViewNode abandoned = mChildNodes.remove(idx); + ViewNode reused = mChildNodes.get(position); mChildNodes.set(idx, reused); - mChildNodes.set(position, abandoned); + mChildNodes.set(position, oldNode); //View swap index mView.removeView(reused.getNodeView()); mView.addView(reused.getNodeView(), Math.min(idx, mView.getChildCount())); - mView.removeView(abandoned.getNodeView()); - mView.addView(abandoned.getNodeView(), position); + mView.removeView(oldNode.getNodeView()); + mView.addView(oldNode.getNodeView(), position); } else { //Not found,insert ViewNode newNode = ViewNode.create(getDoricContext(), type); diff --git a/doric-demo/src/Counter.ts b/doric-demo/src/Counter.ts index 21583ef1..b88cc3fb 100644 --- a/doric-demo/src/Counter.ts +++ b/doric-demo/src/Counter.ts @@ -16,6 +16,7 @@ import { layoutConfig, modal, Panel, + View, } from "doric"; interface CountModel { @@ -25,13 +26,29 @@ class CounterView extends ViewHolder { number!: Text; counter!: Text; build(root: Group) { - vlayout( + let group = vlayout( [ text({ text: `Current language is ${Environment.localeLanguage}`, + onClick: function () { + const v = this as View + group.removeChild(v) + } }), text({ text: `Current country is ${Environment.localeCountry}`, + onClick: function () { + const v = this as View + group.removeChild(v) + } + }), + text({ + text: "Click to remove", + textSize: 30, + onClick: function () { + const v = this as View + group.removeChild(v) + } }), this.number = text({ textSize: 40, diff --git a/doric-iOS/Pod/Classes/Shader/DoricGroupNode.m b/doric-iOS/Pod/Classes/Shader/DoricGroupNode.m index 19ed3628..415faa4d 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricGroupNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricGroupNode.m @@ -97,18 +97,8 @@ - (void)configChildNodes { if (position >= 0) { ///Found ,swap idx,position DoricViewNode *reused = childNodes[(NSUInteger) position]; - [childNodes removeObjectAtIndex:(NSUInteger) position]; - [childNodes removeObjectAtIndex:idx]; - if (idx >= childNodes.count) { - [childNodes addObject:reused]; - } else { - [childNodes insertObject:reused atIndex:idx]; - } - if (position >= childNodes.count) { - [childNodes addObject:oldNode]; - } else { - [childNodes insertObject:oldNode atIndex:(NSUInteger) position]; - } + childNodes[idx] = reused; + childNodes[(NSUInteger) position] = oldNode; ///View swap index [reused.view removeFromSuperview]; [oldNode.view removeFromSuperview];