add supernode to apply layoutconfig

This commit is contained in:
pengfei.zhou 2019-11-13 17:26:26 +08:00
parent 6bf224aa9a
commit 0887ba72e8
8 changed files with 133 additions and 82 deletions

View File

@ -19,7 +19,6 @@ import android.util.SparseArray;
import android.view.ViewGroup;
import pub.doric.DoricContext;
import pub.doric.utils.DoricUtils;
import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSObject;
@ -35,7 +34,7 @@ import java.util.Map;
* @Author: pengfei.zhou
* @CreateDate: 2019-07-20
*/
public abstract class GroupNode<F extends ViewGroup> extends ViewNode<F> {
public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
private Map<String, ViewNode> mChildrenNode = new HashMap<>();
private SparseArray<ViewNode> mIndexInfo = new SparseArray<>();
@ -61,7 +60,7 @@ public abstract class GroupNode<F extends ViewGroup> extends ViewNode<F> {
if (child == null) {
child = ViewNode.create(getDoricContext(), type);
child.index = i;
child.mParent = this;
child.mSuperNode = this;
child.mId = id;
mChildrenNode.put(id, child);
} else {
@ -110,61 +109,4 @@ public abstract class GroupNode<F extends ViewGroup> extends ViewNode<F> {
super.blend(view, layoutParams, name, prop);
}
}
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new ViewGroup.LayoutParams(0, 0);
}
protected void blendChild(ViewNode viewNode, JSObject jsObject) {
JSValue jsValue = jsObject.getProperty("margin");
JSValue widthSpec = jsObject.getProperty("widthSpec");
JSValue heightSpec = jsObject.getProperty("widthSpec");
ViewGroup.LayoutParams layoutParams = viewNode.getLayoutParams();
if (widthSpec.isNumber()) {
switch (widthSpec.asNumber().toInt()) {
case 1:
layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
break;
case 2:
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
break;
default:
break;
}
}
if (heightSpec.isNumber()) {
switch (heightSpec.asNumber().toInt()) {
case 1:
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
break;
case 2:
layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
break;
default:
break;
}
}
if (jsValue.isObject() && layoutParams instanceof ViewGroup.MarginLayoutParams) {
JSValue topVal = jsValue.asObject().getProperty("top");
if (topVal.isNumber()) {
((ViewGroup.MarginLayoutParams) layoutParams).topMargin = DoricUtils.dp2px(topVal.asNumber().toFloat());
}
JSValue leftVal = jsValue.asObject().getProperty("left");
if (leftVal.isNumber()) {
((ViewGroup.MarginLayoutParams) layoutParams).leftMargin = DoricUtils.dp2px(leftVal.asNumber().toFloat());
}
JSValue rightVal = jsValue.asObject().getProperty("right");
if (rightVal.isNumber()) {
((ViewGroup.MarginLayoutParams) layoutParams).rightMargin = DoricUtils.dp2px(rightVal.asNumber().toFloat());
}
JSValue bottomVal = jsValue.asObject().getProperty("bottom");
if (bottomVal.isNumber()) {
((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin = DoricUtils.dp2px(bottomVal.asNumber().toFloat());
}
}
}
}

View File

@ -36,8 +36,8 @@ public class LinearNode extends GroupNode<LinearLayout> {
}
@Override
protected void blendChild(ViewNode viewNode, JSObject layoutConfig) {
super.blendChild(viewNode, layoutConfig);
protected void blendChildLayoutConfig(ViewNode viewNode, JSObject layoutConfig) {
super.blendChildLayoutConfig(viewNode, layoutConfig);
JSValue jsValue = layoutConfig.getProperty("alignment");
if (jsValue.isNumber()) {
((LinearLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt();

View File

@ -36,8 +36,8 @@ public class StackNode extends GroupNode<FrameLayout> {
}
@Override
protected void blendChild(ViewNode viewNode, JSObject jsObject) {
super.blendChild(viewNode, jsObject);
protected void blendChildLayoutConfig(ViewNode viewNode, JSObject jsObject) {
super.blendChildLayoutConfig(viewNode, jsObject);
JSValue jsValue = jsObject.getProperty("alignment");
if (jsValue.isNumber()) {
((FrameLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt();

View File

@ -0,0 +1,91 @@
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.shader;
import android.view.View;
import android.view.ViewGroup;
import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue;
import pub.doric.DoricContext;
import pub.doric.utils.DoricUtils;
/**
* @Description: pub.doric.shader
* @Author: pengfei.zhou
* @CreateDate: 2019-11-13
*/
public abstract class SuperNode<V extends View> extends ViewNode<V> {
public SuperNode(DoricContext doricContext) {
super(doricContext);
}
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new ViewGroup.LayoutParams(0, 0);
}
protected void blendChildLayoutConfig(ViewNode viewNode, JSObject jsObject) {
JSValue jsValue = jsObject.getProperty("margin");
JSValue widthSpec = jsObject.getProperty("widthSpec");
JSValue heightSpec = jsObject.getProperty("heightSpec");
ViewGroup.LayoutParams layoutParams = viewNode.getLayoutParams();
if (widthSpec.isNumber()) {
switch (widthSpec.asNumber().toInt()) {
case 1:
layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
break;
case 2:
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
break;
default:
break;
}
}
if (heightSpec.isNumber()) {
switch (heightSpec.asNumber().toInt()) {
case 1:
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
break;
case 2:
layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
break;
default:
break;
}
}
if (jsValue.isObject() && layoutParams instanceof ViewGroup.MarginLayoutParams) {
JSValue topVal = jsValue.asObject().getProperty("top");
if (topVal.isNumber()) {
((ViewGroup.MarginLayoutParams) layoutParams).topMargin = DoricUtils.dp2px(topVal.asNumber().toFloat());
}
JSValue leftVal = jsValue.asObject().getProperty("left");
if (leftVal.isNumber()) {
((ViewGroup.MarginLayoutParams) layoutParams).leftMargin = DoricUtils.dp2px(leftVal.asNumber().toFloat());
}
JSValue rightVal = jsValue.asObject().getProperty("right");
if (rightVal.isNumber()) {
((ViewGroup.MarginLayoutParams) layoutParams).rightMargin = DoricUtils.dp2px(rightVal.asNumber().toFloat());
}
JSValue bottomVal = jsValue.asObject().getProperty("bottom");
if (bottomVal.isNumber()) {
((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin = DoricUtils.dp2px(bottomVal.asNumber().toFloat());
}
}
}
}

View File

@ -42,7 +42,7 @@ import java.util.LinkedList;
public abstract class ViewNode<T extends View> extends DoricContextHolder {
protected T mView;
int index;
ViewNode mParent;
SuperNode mSuperNode;
String mId;
private ViewGroup.LayoutParams mLayoutParams;
@ -52,8 +52,8 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
private DoricLayer doricLayer;
public void setParentNode(ViewNode parentNode) {
mParent = parentNode;
public void setSuperNode(SuperNode parentNode) {
mSuperNode = parentNode;
}
public View getDoricLayer() {
@ -128,8 +128,8 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
});
break;
case "layoutConfig":
if (prop.isObject() && mParent instanceof GroupNode) {
((GroupNode) mParent).blendChild(this, prop.asObject());
if (prop.isObject() && mSuperNode != null) {
mSuperNode.blendChildLayoutConfig(this, prop.asObject());
}
break;
case "border":
@ -180,7 +180,7 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
ViewNode viewNode = this;
do {
ids.push(viewNode.mId);
viewNode = viewNode.mParent;
viewNode = viewNode.mSuperNode;
} while (viewNode != null && !(viewNode instanceof RootNode));
return ids.toArray(new String[0]);

View File

@ -51,7 +51,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.setParentNode(listNode);
node.setSuperNode(listNode);
return new DoricViewHolder(node, node.getDoricLayer());
}
@ -103,7 +103,7 @@ public class ListAdapter extends RecyclerView.Adapter<ListAdapter.DoricViewHolde
static class DoricViewHolder extends RecyclerView.ViewHolder {
ListItemNode listItemNode;
public DoricViewHolder(ListItemNode node, @NonNull View itemView) {
DoricViewHolder(ListItemNode node, @NonNull View itemView) {
super(itemView);
listItemNode = node;
}

View File

@ -25,6 +25,7 @@ import com.github.pengfeizhou.jscore.JSValue;
import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.shader.SuperNode;
import pub.doric.shader.ViewNode;
/**
@ -33,7 +34,7 @@ import pub.doric.shader.ViewNode;
* @CreateDate: 2019-11-12
*/
@DoricPlugin(name = "List")
public class ListNode extends ViewNode<RecyclerView> {
public class ListNode extends SuperNode<RecyclerView> {
private final ListAdapter listAdapter;
public ListNode(DoricContext doricContext) {
@ -79,4 +80,9 @@ public class ListNode extends ViewNode<RecyclerView> {
break;
}
}
@Override
protected void blendChildLayoutConfig(ViewNode viewNode, JSObject jsObject) {
super.blendChildLayoutConfig(viewNode, jsObject);
}
}

View File

@ -1,4 +1,4 @@
import { Group, Panel, List, text, gravity, Color, Stack, LayoutSpec, ListItem, NativeCall } from "doric";
import { Group, Panel, List, text, gravity, Color, Stack, LayoutSpec, ListItem, NativeCall, listItem } from "doric";
@Entry
class ListPanel extends Panel {
@ -9,17 +9,29 @@ class ListPanel extends Panel {
heightSpec: LayoutSpec.AT_MOST,
}
rootView.addChild(list)
list.itemCount = 10
list.bgColor = Color.parse("#ff00ff")
list.itemCount = 1000
list.renderItem = (idx) => {
const item = new ListItem
item.addChild(text({
width: 100,
height: 100,
return listItem(text({
layoutConfig: {
widthSpec: LayoutSpec.AT_MOST,
heightSpec: LayoutSpec.WRAP_CONTENT,
margin: {
left: 10,
right: 10,
top: 10,
bottom: 10,
},
},
text: `${idx}行内容`,
textAlignment: gravity().center(),
}))
return item
})).also(it => {
it.gravity = gravity().center()
it.bgColor = Color.parse("#fff00f")
it.layoutConfig = {
widthSpec: LayoutSpec.AT_MOST,
heightSpec: LayoutSpec.WRAP_CONTENT,
}
})
}
}
}