feat:add reusable in GroupNode for Android

This commit is contained in:
pengfei.zhou 2019-11-16 11:44:14 +08:00
parent 8e53e4b54b
commit e4b729b2fd
3 changed files with 62 additions and 28 deletions

View File

@ -34,6 +34,8 @@ public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
private ArrayList<ViewNode> mChildNodes = new ArrayList<>(); private ArrayList<ViewNode> mChildNodes = new ArrayList<>();
private ArrayList<String> mChildViewIds = new ArrayList<>(); private ArrayList<String> mChildViewIds = new ArrayList<>();
protected boolean mReusable = false;
public GroupNode(DoricContext doricContext) { public GroupNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
} }
@ -66,6 +68,26 @@ public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
ViewNode oldNode = mChildNodes.get(idx); ViewNode oldNode = mChildNodes.get(idx);
if (id.equals(oldNode.getId())) { if (id.equals(oldNode.getId())) {
//The same,skip //The same,skip
} else {
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());
}
} else { } else {
//Find in remain nodes //Find in remain nodes
int position = -1; int position = -1;
@ -99,10 +121,14 @@ public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
mView.addView(newNode.getDoricLayer(), idx, newNode.getLayoutParams()); mView.addView(newNode.getDoricLayer(), idx, newNode.getLayoutParams());
} }
} }
}
} else { } else {
//Insert //Insert
ViewNode newNode = ViewNode.create(getDoricContext(), type); ViewNode newNode = ViewNode.create(getDoricContext(), type);
newNode.setId(id); newNode.setId(id);
if (newNode instanceof GroupNode) {
((GroupNode) newNode).mReusable = this.mReusable;
}
newNode.init(this); newNode.init(this);
newNode.blend(model.getProperty("props").asObject()); newNode.blend(model.getProperty("props").asObject());
mChildNodes.add(newNode); mChildNodes.add(newNode);

View File

@ -44,6 +44,7 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
SuperNode mSuperNode; SuperNode mSuperNode;
String mId; String mId;
protected ViewGroup.LayoutParams mLayoutParams; protected ViewGroup.LayoutParams mLayoutParams;
private String mType;
public ViewNode(DoricContext doricContext) { public ViewNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
@ -65,6 +66,10 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
this.mId = id; this.mId = id;
} }
public String getType(){
return mType;
}
public View getDoricLayer() { public View getDoricLayer() {
return doricLayer; return doricLayer;
} }
@ -199,7 +204,9 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
public static ViewNode create(DoricContext doricContext, String type) { public static ViewNode create(DoricContext doricContext, String type) {
DoricRegistry registry = doricContext.getDriver().getRegistry(); DoricRegistry registry = doricContext.getDriver().getRegistry();
DoricMetaInfo<ViewNode> clz = registry.acquireViewNodeInfo(type); DoricMetaInfo<ViewNode> clz = registry.acquireViewNodeInfo(type);
return clz.createInstance(doricContext); ViewNode ret = clz.createInstance(doricContext);
ret.mType = type;
return ret;
} }
public ViewGroup.LayoutParams getLayoutParams() { public ViewGroup.LayoutParams getLayoutParams() {

View File

@ -34,6 +34,7 @@ public class ListItemNode extends StackNode {
public ListItemNode(DoricContext doricContext) { public ListItemNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
this.mReusable = true;
} }
@Override @Override