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);
} }
@ -67,42 +69,66 @@ public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
if (id.equals(oldNode.getId())) { if (id.equals(oldNode.getId())) {
//The same,skip //The same,skip
} else { } else {
//Find in remain nodes if (mReusable) {
int position = -1; if (oldNode.getType().equals(type)) {
for (int start = idx + 1; start < mChildNodes.size(); start++) { //Same type,can be reused
ViewNode node = mChildNodes.get(start); oldNode.setId(id);
if (id.equals(node.getId())) { oldNode.blend(model.getProperty("props").asObject());
//Found } else {
position = start; //Replace this view
break; 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 { } else {
//Not found,insert //Find in remain nodes
ViewNode newNode = ViewNode.create(getDoricContext(), type); int position = -1;
newNode.setId(id); for (int start = idx + 1; start < mChildNodes.size(); start++) {
newNode.init(this); ViewNode node = mChildNodes.get(start);
newNode.blend(model.getProperty("props").asObject()); 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); mChildNodes.add(idx, newNode);
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