add node init method

This commit is contained in:
pengfei.zhou 2019-11-14 18:55:38 +08:00
parent 9e505894b0
commit e3d07dc3ef
12 changed files with 48 additions and 71 deletions

View File

@ -39,7 +39,7 @@ public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
}
@Override
protected void blend(F view, ViewGroup.LayoutParams layoutParams, String name, JSValue prop) {
protected void blend(F view, String name, JSValue prop) {
if ("children".equals(name)) {
JSArray ids = prop.asArray();
mChildViewIds.clear();
@ -47,13 +47,13 @@ public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
mChildViewIds.add(ids.get(i).asString().value());
}
} else {
super.blend(view, layoutParams, name, prop);
super.blend(view, name, prop);
}
}
@Override
public void blend(JSObject jsObject, ViewGroup.LayoutParams layoutParams) {
super.blend(jsObject, layoutParams);
public void blend(JSObject jsObject) {
super.blend(jsObject);
configChildNode();
}
@ -91,10 +91,9 @@ public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
} else {
//Not found,insert
ViewNode newNode = ViewNode.create(getDoricContext(), type);
newNode.setSuperNode(this);
newNode.setId(id);
ViewGroup.LayoutParams params = generateDefaultLayoutParams();
newNode.blend(model.getProperty("props").asObject(), params);
newNode.init(this);
newNode.blend(model.getProperty("props").asObject());
mChildNodes.set(idx, newNode);
mView.addView(newNode.getDoricLayer(), idx, newNode.getLayoutParams());
@ -103,10 +102,9 @@ public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
} else {
//Insert
ViewNode newNode = ViewNode.create(getDoricContext(), type);
newNode.setSuperNode(this);
newNode.setId(id);
ViewGroup.LayoutParams params = generateDefaultLayoutParams();
newNode.blend(model.getProperty("props").asObject(), params);
newNode.init(this);
newNode.blend(model.getProperty("props").asObject());
mChildNodes.add(newNode);
mView.addView(newNode.getDoricLayer(), idx, newNode.getLayoutParams());
}
@ -123,7 +121,7 @@ public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
String subNodeId = subProp.getProperty("id").asString().value();
for (ViewNode node : mChildNodes) {
if (subNodeId.equals(node.getId())) {
node.blend(subProp.getProperty("props").asObject(), node.getLayoutParams());
node.blend(subProp.getProperty("props").asObject());
break;
}
}

View File

@ -51,7 +51,7 @@ public class ImageNode extends ViewNode<ImageView> {
}
@Override
protected void blend(ImageView view, ViewGroup.LayoutParams layoutParams, String name, JSValue prop) {
protected void blend(ImageView view, String name, JSValue prop) {
if ("imageUrl".equals(name)) {
Glide.with(getContext()).load(prop.asString().value())
.listener(new RequestListener<Drawable>() {
@ -67,7 +67,7 @@ public class ImageNode extends ViewNode<ImageView> {
})
.into(view);
} else {
super.blend(view, layoutParams, name, prop);
super.blend(view, name, prop);
}
}
}

View File

@ -59,7 +59,7 @@ public class LinearNode extends GroupNode<LinearLayout> {
}
@Override
protected void blend(LinearLayout view, ViewGroup.LayoutParams params, String name, JSValue prop) {
protected void blend(LinearLayout view, String name, JSValue prop) {
switch (name) {
case "space":
ShapeDrawable shapeDrawable;
@ -82,7 +82,7 @@ public class LinearNode extends GroupNode<LinearLayout> {
view.setGravity(prop.asNumber().toInt());
break;
default:
super.blend(view, params, name, prop);
super.blend(view, name, prop);
break;
}
}

View File

@ -42,6 +42,11 @@ public class RootNode extends StackNode {
public void setRootView(FrameLayout rootView) {
this.mView = rootView;
mLayoutParams = rootView.getLayoutParams();
if (mLayoutParams == null) {
mLayoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootView.setLayoutParams(mLayoutParams);
}
}
@Override
@ -50,6 +55,6 @@ public class RootNode extends StackNode {
}
public void render(JSObject props) {
blend(props, getLayoutParams());
blend(props);
}
}

View File

@ -50,13 +50,13 @@ public class StackNode extends GroupNode<FrameLayout> {
}
@Override
protected void blend(FrameLayout view, ViewGroup.LayoutParams layoutParams, String name, JSValue prop) {
protected void blend(FrameLayout view, String name, JSValue prop) {
switch (name) {
case "gravity":
view.setForegroundGravity(prop.asNumber().toInt());
break;
default:
super.blend(view, layoutParams, name, prop);
super.blend(view, name, prop);
}
}

View File

@ -45,7 +45,7 @@ public abstract class SuperNode<V extends View> extends ViewNode<V> {
}
@Override
protected void blend(V view, ViewGroup.LayoutParams layoutParams, String name, JSValue prop) {
protected void blend(V view, String name, JSValue prop) {
if (name.equals("subviews")) {
if (prop.isArray()) {
JSArray subviews = prop.asArray();
@ -56,7 +56,7 @@ public abstract class SuperNode<V extends View> extends ViewNode<V> {
}
}
} else {
super.blend(view, layoutParams, name, prop);
super.blend(view, name, prop);
}
}

View File

@ -16,13 +16,11 @@
package pub.doric.shader;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.TextView;
import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin;
import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue;
/**
@ -42,7 +40,7 @@ public class TextNode extends ViewNode<TextView> {
}
@Override
protected void blend(TextView view, ViewGroup.LayoutParams params, String name, JSValue prop) {
protected void blend(TextView view, String name, JSValue prop) {
switch (name) {
case "text":
view.setText(prop.asString().toString());
@ -57,7 +55,7 @@ public class TextNode extends ViewNode<TextView> {
view.setGravity(prop.asNumber().toInt());
break;
default:
super.blend(view, params, name, prop);
super.blend(view, name, prop);
break;
}
}

View File

@ -20,8 +20,6 @@ import android.widget.LinearLayout;
import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin;
import com.github.pengfeizhou.jscore.JSObject;
/**
* @Description: com.github.penfeizhou.doric.shader
* @Author: pengfei.zhou

View File

@ -43,7 +43,7 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
protected T mView;
SuperNode mSuperNode;
String mId;
private ViewGroup.LayoutParams mLayoutParams;
protected ViewGroup.LayoutParams mLayoutParams;
public ViewNode(DoricContext doricContext) {
super(doricContext);
@ -60,11 +60,6 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
doricLayer.addView(mView, params);
}
public void setSuperNode(SuperNode parentNode) {
mSuperNode = parentNode;
}
public void setId(String id) {
this.mId = id;
}
@ -79,53 +74,44 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
protected abstract T build();
public void blend(JSObject jsObject, ViewGroup.LayoutParams layoutParams) {
mLayoutParams = layoutParams;
if (mView == null) {
mView = build();
}
if (getDoricLayer() == null) {
doricLayer = new DoricLayer(getContext());
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(layoutParams.width, layoutParams.height);
doricLayer.addView(mView, params);
}
public void blend(JSObject jsObject) {
if (jsObject != null) {
for (String prop : jsObject.propertySet()) {
blend(mView, layoutParams, prop, jsObject.getProperty(prop));
blend(mView, prop, jsObject.getProperty(prop));
}
}
ViewGroup.LayoutParams params = mView.getLayoutParams();
if (params != null) {
params.width = layoutParams.width;
params.height = layoutParams.height;
params.width = mLayoutParams.width;
params.height = mLayoutParams.height;
} else {
params = layoutParams;
params = mLayoutParams;
}
mView.setLayoutParams(params);
}
protected void blend(T view, ViewGroup.LayoutParams layoutParams, String name, JSValue prop) {
protected void blend(T view, String name, JSValue prop) {
switch (name) {
case "width":
if (layoutParams.width >= 0) {
layoutParams.width = DoricUtils.dp2px(prop.asNumber().toFloat());
if (mLayoutParams.width >= 0) {
mLayoutParams.width = DoricUtils.dp2px(prop.asNumber().toFloat());
}
break;
case "height":
if (layoutParams.height >= 0) {
layoutParams.height = DoricUtils.dp2px(prop.asNumber().toFloat());
if (mLayoutParams.height >= 0) {
mLayoutParams.height = DoricUtils.dp2px(prop.asNumber().toFloat());
}
break;
case "x":
if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
if (mLayoutParams instanceof ViewGroup.MarginLayoutParams) {
float x = prop.asNumber().toFloat();
((ViewGroup.MarginLayoutParams) layoutParams).leftMargin = DoricUtils.dp2px(x);
((ViewGroup.MarginLayoutParams) mLayoutParams).leftMargin = DoricUtils.dp2px(x);
}
break;
case "y":
if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
if (mLayoutParams instanceof ViewGroup.MarginLayoutParams) {
float y = prop.asNumber().toFloat();
((ViewGroup.MarginLayoutParams) layoutParams).topMargin = DoricUtils.dp2px(y);
((ViewGroup.MarginLayoutParams) mLayoutParams).topMargin = DoricUtils.dp2px(y);
}
break;
case "bgColor":

View File

@ -52,7 +52,7 @@ public class ListAdapter extends RecyclerView.Adapter<ListAdapter.DoricViewHolde
@Override
public DoricViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ListItemNode node = (ListItemNode) ViewNode.create(listNode.getDoricContext(), "ListItem");
node.setSuperNode(listNode);
node.init(listNode);
return new DoricViewHolder(node, node.getDoricLayer());
}
@ -62,7 +62,7 @@ public class ListAdapter extends RecyclerView.Adapter<ListAdapter.DoricViewHolde
if (jsValue.isObject()) {
JSObject jsObject = jsValue.asObject();
holder.listItemNode.setId(jsObject.getProperty("id").asString().value());
holder.listItemNode.blend(jsObject.getProperty("props").asObject(), holder.itemView.getLayoutParams());
holder.listItemNode.blend(jsObject.getProperty("props").asObject());
}
}

View File

@ -15,7 +15,6 @@
*/
package pub.doric.shader.list;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.github.pengfeizhou.jscore.JSValue;
@ -35,19 +34,14 @@ public class ListItemNode extends StackNode {
public ListItemNode(DoricContext doricContext) {
super(doricContext);
this.mView = this.build();
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
this.blend(null, params);
getDoricLayer().setLayoutParams(params);
}
@Override
protected void blend(FrameLayout view, ViewGroup.LayoutParams layoutParams, String name, JSValue prop) {
protected void blend(FrameLayout view, String name, JSValue prop) {
if ("identifier".equals(name)) {
this.identifier = prop.asString().value();
} else {
super.blend(view, layoutParams, name, prop);
super.blend(view, name, prop);
}
}
}

View File

@ -15,8 +15,6 @@
*/
package pub.doric.shader.list;
import android.view.ViewGroup;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
@ -56,8 +54,8 @@ public class ListNode extends SuperNode<RecyclerView> {
}
@Override
public void blend(JSObject jsObject, ViewGroup.LayoutParams layoutParams) {
super.blend(jsObject, layoutParams);
public void blend(JSObject jsObject) {
super.blend(jsObject);
if (mView != null) {
mView.post(new Runnable() {
@Override
@ -69,7 +67,7 @@ public class ListNode extends SuperNode<RecyclerView> {
}
@Override
protected void blend(RecyclerView view, ViewGroup.LayoutParams layoutParams, String name, JSValue prop) {
protected void blend(RecyclerView view, String name, JSValue prop) {
switch (name) {
case "itemCount":
this.listAdapter.itemCount = prop.asNumber().toInt();
@ -83,7 +81,7 @@ public class ListNode extends SuperNode<RecyclerView> {
this.listAdapter.batchCount = 15;
break;
default:
super.blend(view, layoutParams, name, prop);
super.blend(view, name, prop);
break;
}
}