Group and view
This commit is contained in:
@@ -5,7 +5,9 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.github.penfeizhou.doric.DoricContext;
|
||||
import com.github.penfeizhou.doric.DoricRegistry;
|
||||
import com.github.penfeizhou.doric.utils.DoricComponent;
|
||||
import com.github.penfeizhou.doric.utils.DoricMetaInfo;
|
||||
import com.github.penfeizhou.doric.utils.DoricUtils;
|
||||
import com.github.pengfeizhou.jscore.JSObject;
|
||||
|
||||
@@ -15,18 +17,32 @@ import com.github.pengfeizhou.jscore.JSObject;
|
||||
* @CreateDate: 2019-07-20
|
||||
*/
|
||||
public abstract class ViewNode<T extends View> extends DoricComponent {
|
||||
private T mView;
|
||||
private String mId;
|
||||
|
||||
public ViewNode(DoricContext doricContext) {
|
||||
super(doricContext);
|
||||
}
|
||||
|
||||
public abstract T build(JSObject jsObject);
|
||||
public T getView() {
|
||||
return mView;
|
||||
}
|
||||
|
||||
private void setId(String id) {
|
||||
mId = id;
|
||||
}
|
||||
|
||||
public Context getContext() {
|
||||
return getDoricContext().getContext();
|
||||
}
|
||||
|
||||
public void config(T view, JSObject jsObject) {
|
||||
setFrame(view.getLayoutParams(), jsObject);
|
||||
public abstract T build(JSObject jsObject);
|
||||
|
||||
public void blend(JSObject jsObject) {
|
||||
if (mView == null) {
|
||||
mView = build(jsObject);
|
||||
}
|
||||
setFrame(mView.getLayoutParams(), jsObject);
|
||||
}
|
||||
|
||||
public void setFrame(ViewGroup.LayoutParams layoutParams, JSObject jsObject) {
|
||||
@@ -41,4 +57,12 @@ public abstract class ViewNode<T extends View> extends DoricComponent {
|
||||
((ViewGroup.MarginLayoutParams) layoutParams).topMargin = DoricUtils.dp2px(y);
|
||||
}
|
||||
}
|
||||
|
||||
public static ViewNode create(DoricContext doricContext, String id, String type) {
|
||||
DoricRegistry registry = doricContext.getDriver().getRegistry();
|
||||
DoricMetaInfo<ViewNode> clz = registry.acquireViewNodeInfo(type);
|
||||
ViewNode node = clz.createInstance(doricContext);
|
||||
node.setId(id);
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
@@ -7,21 +7,38 @@ import com.github.penfeizhou.doric.extension.render.ViewNode;
|
||||
import com.github.pengfeizhou.jscore.JSArray;
|
||||
import com.github.pengfeizhou.jscore.JSObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Description: com.github.penfeizhou.doric.widget
|
||||
* @Author: pengfei.zhou
|
||||
* @CreateDate: 2019-07-20
|
||||
*/
|
||||
public abstract class GroupNode extends ViewNode<ViewGroup> {
|
||||
private Map<String, ViewNode> mChildren = new HashMap<>();
|
||||
|
||||
public GroupNode(DoricContext doricContext) {
|
||||
super(doricContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void config(ViewGroup view, JSObject jsObject) {
|
||||
super.config(view, jsObject);
|
||||
public void blend(JSObject jsObject) {
|
||||
super.blend(jsObject);
|
||||
JSArray jsArray = jsObject.getProperty("children").asArray();
|
||||
for (int i = 0; i < jsArray.size(); i++) {
|
||||
JSObject childObj = jsArray.get(i).asObject();
|
||||
String type = childObj.getProperty("type").asString().value();
|
||||
String id = childObj.getProperty("id").asString().value();
|
||||
ViewNode child = mChildren.get(id);
|
||||
if (child == null) {
|
||||
child = ViewNode.create(getDoricContext(), id, type);
|
||||
mChildren.put(id, child);
|
||||
}
|
||||
if (getView().getChildAt(i) == null) {
|
||||
getView().addView(child.getView());
|
||||
}
|
||||
child.blend(childObj.getProperty("props").asObject());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user