From e4b729b2fd12f1d23819f6acc8a108de56fd9ec3 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 16 Nov 2019 11:44:14 +0800 Subject: [PATCH] feat:add reusable in GroupNode for Android --- .../main/java/pub/doric/shader/GroupNode.java | 80 ++++++++++++------- .../main/java/pub/doric/shader/ViewNode.java | 9 ++- .../pub/doric/shader/list/ListItemNode.java | 1 + 3 files changed, 62 insertions(+), 28 deletions(-) diff --git a/Android/doric/src/main/java/pub/doric/shader/GroupNode.java b/Android/doric/src/main/java/pub/doric/shader/GroupNode.java index edcd969e..5d6c9b3a 100644 --- a/Android/doric/src/main/java/pub/doric/shader/GroupNode.java +++ b/Android/doric/src/main/java/pub/doric/shader/GroupNode.java @@ -34,6 +34,8 @@ public abstract class GroupNode extends SuperNode { private ArrayList mChildNodes = new ArrayList<>(); private ArrayList mChildViewIds = new ArrayList<>(); + protected boolean mReusable = false; + public GroupNode(DoricContext doricContext) { super(doricContext); } @@ -67,42 +69,66 @@ public abstract class GroupNode extends SuperNode { if (id.equals(oldNode.getId())) { //The same,skip } else { - //Find in remain nodes - int position = -1; - for (int start = idx + 1; start < mChildNodes.size(); start++) { - ViewNode node = mChildNodes.get(start); - if (id.equals(node.getId())) { - //Found - position = start; - break; + if (mReusable) { + if (oldNode.getType().equals(type)) { + //Same type,can be reused + oldNode.setId(id); + oldNode.blend(model.getProperty("props").asObject()); + } else { + //Replace this view + mChildNodes.remove(idx); + mView.removeView(oldNode.getDoricLayer()); + ViewNode newNode = ViewNode.create(getDoricContext(), type); + newNode.setId(id); + if (newNode instanceof GroupNode) { + ((GroupNode) newNode).mReusable = this.mReusable; + } + newNode.init(this); + newNode.blend(model.getProperty("props").asObject()); + mChildNodes.add(idx, newNode); + mView.addView(newNode.getDoricLayer(), idx, newNode.getLayoutParams()); } - } - if (position >= 0) { - //Found swap idx,position - ViewNode reused = mChildNodes.remove(position); - ViewNode abandoned = mChildNodes.remove(idx); - mChildNodes.set(idx, reused); - mChildNodes.set(position, abandoned); - //View swap index - mView.removeView(reused.getDoricLayer()); - mView.addView(reused.getDoricLayer(), idx); - mView.removeView(abandoned.getDoricLayer()); - mView.addView(abandoned.getDoricLayer(), position); } else { - //Not found,insert - ViewNode newNode = ViewNode.create(getDoricContext(), type); - newNode.setId(id); - newNode.init(this); - newNode.blend(model.getProperty("props").asObject()); + //Find in remain nodes + int position = -1; + for (int start = idx + 1; start < mChildNodes.size(); start++) { + ViewNode node = mChildNodes.get(start); + if (id.equals(node.getId())) { + //Found + position = start; + break; + } + } + if (position >= 0) { + //Found swap idx,position + ViewNode reused = mChildNodes.remove(position); + ViewNode abandoned = mChildNodes.remove(idx); + mChildNodes.set(idx, reused); + mChildNodes.set(position, abandoned); + //View swap index + mView.removeView(reused.getDoricLayer()); + mView.addView(reused.getDoricLayer(), idx); + mView.removeView(abandoned.getDoricLayer()); + mView.addView(abandoned.getDoricLayer(), position); + } else { + //Not found,insert + ViewNode newNode = ViewNode.create(getDoricContext(), type); + newNode.setId(id); + newNode.init(this); + newNode.blend(model.getProperty("props").asObject()); - mChildNodes.add(idx, newNode); - mView.addView(newNode.getDoricLayer(), idx, newNode.getLayoutParams()); + mChildNodes.add(idx, newNode); + mView.addView(newNode.getDoricLayer(), idx, newNode.getLayoutParams()); + } } } } else { //Insert ViewNode newNode = ViewNode.create(getDoricContext(), type); newNode.setId(id); + if (newNode instanceof GroupNode) { + ((GroupNode) newNode).mReusable = this.mReusable; + } newNode.init(this); newNode.blend(model.getProperty("props").asObject()); mChildNodes.add(newNode); diff --git a/Android/doric/src/main/java/pub/doric/shader/ViewNode.java b/Android/doric/src/main/java/pub/doric/shader/ViewNode.java index bbf71432..3faae261 100644 --- a/Android/doric/src/main/java/pub/doric/shader/ViewNode.java +++ b/Android/doric/src/main/java/pub/doric/shader/ViewNode.java @@ -44,6 +44,7 @@ public abstract class ViewNode extends DoricContextHolder { SuperNode mSuperNode; String mId; protected ViewGroup.LayoutParams mLayoutParams; + private String mType; public ViewNode(DoricContext doricContext) { super(doricContext); @@ -65,6 +66,10 @@ public abstract class ViewNode extends DoricContextHolder { this.mId = id; } + public String getType(){ + return mType; + } + public View getDoricLayer() { return doricLayer; } @@ -199,7 +204,9 @@ public abstract class ViewNode extends DoricContextHolder { public static ViewNode create(DoricContext doricContext, String type) { DoricRegistry registry = doricContext.getDriver().getRegistry(); DoricMetaInfo clz = registry.acquireViewNodeInfo(type); - return clz.createInstance(doricContext); + ViewNode ret = clz.createInstance(doricContext); + ret.mType = type; + return ret; } public ViewGroup.LayoutParams getLayoutParams() { diff --git a/Android/doric/src/main/java/pub/doric/shader/list/ListItemNode.java b/Android/doric/src/main/java/pub/doric/shader/list/ListItemNode.java index 76fdfe3d..8ce9fcf6 100644 --- a/Android/doric/src/main/java/pub/doric/shader/list/ListItemNode.java +++ b/Android/doric/src/main/java/pub/doric/shader/list/ListItemNode.java @@ -34,6 +34,7 @@ public class ListItemNode extends StackNode { public ListItemNode(DoricContext doricContext) { super(doricContext); + this.mReusable = true; } @Override