fix add child to group,not make root dirty

This commit is contained in:
pengfei.zhou
2019-07-24 12:48:30 +08:00
parent ac5dc091d5
commit f33c320b0c
7 changed files with 97 additions and 76 deletions

View File

@@ -1,7 +1,6 @@
package com.github.penfeizhou.doric.shader;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import com.github.penfeizhou.doric.DoricContext;
@@ -26,50 +25,59 @@ public abstract class GroupNode<F extends ViewGroup> extends ViewNode<F> {
}
@Override
public void blend(JSObject jsObject, ViewGroup.LayoutParams layoutParams) {
super.blend(jsObject, layoutParams);
JSArray jsArray = jsObject.getProperty("children").asArray();
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 = mChildrenNode.get(id);
if (child == null) {
child = ViewNode.create(getDoricContext(), type);
child.index = i;
child.mParent = this;
child.mId = id;
mChildrenNode.put(id, child);
} else if (i != child.index) {
mIndexInfo.remove(i);
child.index = i;
mView.removeView(child.mView);
}
ViewGroup.LayoutParams params = child.getLayoutParams();
if (params == null) {
params = generateDefaultLayoutParams();
}
child.blend(childObj.getProperty("props").asObject(), params);
if (mIndexInfo.get(i) == null) {
mView.addView(child.mView, i);
mIndexInfo.put(i, child);
}
}
while (i < mView.getChildCount()) {
mView.removeViewAt(mView.getChildCount() - 1);
if (mIndexInfo.get(i) != null) {
mChildrenNode.remove(mIndexInfo.get(i).getId());
mIndexInfo.remove(i);
}
protected void blend(F view, ViewGroup.LayoutParams layoutParams, String name, JSValue prop) {
super.blend(view, layoutParams, name, prop);
switch (name) {
case "children":
JSArray jsArray = prop.asArray();
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 = mChildrenNode.get(id);
if (child == null) {
child = ViewNode.create(getDoricContext(), type);
child.index = i;
child.mParent = this;
child.mId = id;
mChildrenNode.put(id, child);
} else if (i != child.index) {
mIndexInfo.remove(i);
child.index = i;
mView.removeView(child.mView);
}
ViewGroup.LayoutParams params = child.getLayoutParams();
if (params == null) {
params = generateDefaultLayoutParams();
}
child.blend(childObj.getProperty("props").asObject(), params);
if (mIndexInfo.get(i) == null) {
mView.addView(child.mView, i);
mIndexInfo.put(i, child);
}
}
while (i < mView.getChildCount()) {
mView.removeViewAt(mView.getChildCount() - 1);
if (mIndexInfo.get(i) != null) {
mChildrenNode.remove(mIndexInfo.get(i).getId());
mIndexInfo.remove(i);
}
}
break;
default:
super.blend(view, layoutParams, name, prop);
break;
}
}
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
public abstract void blendChild(ViewNode viewNode, JSObject jsObject);
}

View File

@@ -19,6 +19,14 @@ public class LinearNode extends GroupNode<LinearLayout> {
super(doricContext);
}
@Override
public void blendChild(ViewNode viewNode, JSObject layoutConfig) {
JSValue jsValue = layoutConfig.getProperty("alignment");
if (jsValue.isNumber()) {
((LinearLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt();
}
}
@Override
public LinearLayout build(JSObject jsObject) {
return new LinearLayout(getContext());

View File

@@ -12,16 +12,11 @@ import com.github.pengfeizhou.jscore.JSObject;
* @CreateDate: 2019-07-20
*/
@DoricPlugin(name = "Root")
public class RootNode extends GroupNode<FrameLayout> {
public class RootNode extends StackNode {
public RootNode(DoricContext doricContext) {
super(doricContext);
}
@Override
public FrameLayout build(JSObject jsObject) {
return new FrameLayout(getContext());
}
public void setRootView(FrameLayout rootView) {
this.mView = rootView;
}

View File

@@ -19,6 +19,14 @@ public class StackNode extends GroupNode<FrameLayout> {
super(doricContext);
}
@Override
public void blendChild(ViewNode viewNode, JSObject jsObject) {
JSValue jsValue = jsObject.getProperty("alignment");
if (jsValue.isNumber()) {
((FrameLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt();
}
}
@Override
public FrameLayout build(JSObject jsObject) {
return new FrameLayout(getContext());
@@ -34,4 +42,9 @@ public class StackNode extends GroupNode<FrameLayout> {
super.blend(view, layoutParams, name, prop);
}
}
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
}

View File

@@ -25,8 +25,9 @@ import java.util.LinkedList;
public abstract class ViewNode<T extends View> extends DoricComponent {
protected T mView;
int index;
ViewNode<ViewGroup> mParent;
GroupNode mParent;
String mId;
private ViewGroup.LayoutParams mLayoutParams;
public ViewNode(DoricContext doricContext) {
super(doricContext);
@@ -43,6 +44,7 @@ public abstract class ViewNode<T extends View> extends DoricComponent {
public abstract T build(JSObject jsObject);
void blend(JSObject jsObject, ViewGroup.LayoutParams layoutParams) {
mLayoutParams = layoutParams;
if (mView == null) {
mView = build(jsObject);
}
@@ -93,14 +95,8 @@ public abstract class ViewNode<T extends View> extends DoricComponent {
});
break;
case "layoutConfig":
JSObject layoutConfig = prop.asObject();
JSValue jsValue = layoutConfig.getProperty("alignment");
if (jsValue.isNumber()) {
if (layoutParams instanceof LinearLayout.LayoutParams) {
((LinearLayout.LayoutParams) layoutParams).gravity = jsValue.asNumber().toInt();
} else if (layoutParams instanceof FrameLayout.LayoutParams) {
((FrameLayout.LayoutParams) layoutParams).gravity = jsValue.asNumber().toInt();
}
if (prop.isObject() && mParent != null) {
mParent.blendChild(this, prop.asObject());
}
break;
default:
@@ -136,10 +132,7 @@ public abstract class ViewNode<T extends View> extends DoricComponent {
}
public ViewGroup.LayoutParams getLayoutParams() {
if (mView != null) {
return mView.getLayoutParams();
}
return null;
return mLayoutParams;
}
public String getId() {