diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricRegistry.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricRegistry.java index ec8905d9..fcc85189 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricRegistry.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricRegistry.java @@ -2,7 +2,7 @@ package com.github.penfeizhou.doric; import android.text.TextUtils; -import com.github.penfeizhou.doric.extension.render.ViewNode; +import com.github.penfeizhou.doric.widget.ViewNode; import com.github.penfeizhou.doric.utils.DoricMetaInfo; import com.github.penfeizhou.doric.plugin.DoricJavaPlugin; import com.github.penfeizhou.doric.plugin.ModalPlugin; diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/GroupNode.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/GroupNode.java index 3b47e44b..3abb40fc 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/GroupNode.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/GroupNode.java @@ -1,11 +1,12 @@ package com.github.penfeizhou.doric.widget; +import android.util.SparseArray; import android.view.ViewGroup; import com.github.penfeizhou.doric.DoricContext; -import com.github.penfeizhou.doric.extension.render.ViewNode; import com.github.pengfeizhou.jscore.JSArray; import com.github.pengfeizhou.jscore.JSObject; +import com.github.pengfeizhou.jscore.JSValue; import java.util.HashMap; import java.util.Map; @@ -16,7 +17,8 @@ import java.util.Map; * @CreateDate: 2019-07-20 */ public abstract class GroupNode extends ViewNode { - private Map mChildren = new HashMap<>(); + private Map mChildrenNode = new HashMap<>(); + private SparseArray mIndexInfo = new SparseArray<>(); public GroupNode(DoricContext doricContext) { super(doricContext); @@ -26,19 +28,31 @@ public abstract class GroupNode extends ViewNode { public void blend(JSObject jsObject) { super.blend(jsObject); JSArray jsArray = jsObject.getProperty("children").asArray(); - for (int i = 0; i < jsArray.size(); i++) { - JSObject childObj = jsArray.get(i).asObject(); + int i; + for (i = 0; i < jsArray.size(); i++) { + JSValue jsValue = jsArray.get(i); + if (!jsValue.isObject()) { + continue; + } + JSObject childObj = jsValue.asObject(); String type = childObj.getProperty("type").asString().value(); String id = childObj.getProperty("id").asString().value(); - ViewNode child = mChildren.get(id); + ViewNode child = mChildrenNode.get(id); if (child == null) { child = ViewNode.create(getDoricContext(), id, type); - mChildren.put(id, child); - } - if (getView().getChildAt(i) == null) { - getView().addView(child.getView()); + child.index = i; + mChildrenNode.put(id, child); + mView.addView(child.mView, i); + } else if (i != child.index) { + mView.removeView(child.mView); + mView.addView(child.mView, i); } child.blend(childObj.getProperty("props").asObject()); + mIndexInfo.put(i, child); + } + while (i < mView.getChildCount()) { + mView.removeViewAt(mView.getChildCount() - 1); + mChildrenNode.remove(mIndexInfo.get(i).id); } } } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/ImageNode.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/ImageNode.java index 2a625cdc..36918c52 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/ImageNode.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/ImageNode.java @@ -4,7 +4,6 @@ import android.widget.ImageView; import com.github.penfeizhou.doric.DoricContext; import com.github.penfeizhou.doric.extension.render.DoricNode; -import com.github.penfeizhou.doric.extension.render.ViewNode; import com.github.pengfeizhou.jscore.JSObject; /** diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/TextNode.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/TextNode.java index 01116050..0352cb54 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/TextNode.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/TextNode.java @@ -4,7 +4,6 @@ import android.widget.TextView; import com.github.penfeizhou.doric.DoricContext; import com.github.penfeizhou.doric.extension.render.DoricNode; -import com.github.penfeizhou.doric.extension.render.ViewNode; import com.github.pengfeizhou.jscore.JSObject; /** diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/render/ViewNode.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/ViewNode.java similarity index 91% rename from Android/doric/src/main/java/com/github/penfeizhou/doric/extension/render/ViewNode.java rename to Android/doric/src/main/java/com/github/penfeizhou/doric/widget/ViewNode.java index 9bbc3fcc..21cd7362 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/render/ViewNode.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/ViewNode.java @@ -1,4 +1,4 @@ -package com.github.penfeizhou.doric.extension.render; +package com.github.penfeizhou.doric.widget; import android.content.Context; import android.view.View; @@ -17,8 +17,10 @@ import com.github.pengfeizhou.jscore.JSObject; * @CreateDate: 2019-07-20 */ public abstract class ViewNode extends DoricComponent { - private T mView; - private String mId; + protected T mView; + String id; + + int index; public ViewNode(DoricContext doricContext) { super(doricContext); @@ -28,10 +30,6 @@ public abstract class ViewNode extends DoricComponent { return mView; } - private void setId(String id) { - mId = id; - } - public Context getContext() { return getDoricContext().getContext(); } @@ -62,7 +60,7 @@ public abstract class ViewNode extends DoricComponent { DoricRegistry registry = doricContext.getDriver().getRegistry(); DoricMetaInfo clz = registry.acquireViewNodeInfo(type); ViewNode node = clz.createInstance(doricContext); - node.setId(id); + node.id = id; return node; } }