add supernode to apply layoutconfig
This commit is contained in:
parent
6bf224aa9a
commit
0887ba72e8
@ -19,7 +19,6 @@ import android.util.SparseArray;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
import pub.doric.DoricContext;
|
import pub.doric.DoricContext;
|
||||||
import pub.doric.utils.DoricUtils;
|
|
||||||
|
|
||||||
import com.github.pengfeizhou.jscore.JSArray;
|
import com.github.pengfeizhou.jscore.JSArray;
|
||||||
import com.github.pengfeizhou.jscore.JSObject;
|
import com.github.pengfeizhou.jscore.JSObject;
|
||||||
@ -35,7 +34,7 @@ import java.util.Map;
|
|||||||
* @Author: pengfei.zhou
|
* @Author: pengfei.zhou
|
||||||
* @CreateDate: 2019-07-20
|
* @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 Map<String, ViewNode> mChildrenNode = new HashMap<>();
|
||||||
private SparseArray<ViewNode> mIndexInfo = new SparseArray<>();
|
private SparseArray<ViewNode> mIndexInfo = new SparseArray<>();
|
||||||
|
|
||||||
@ -61,7 +60,7 @@ public abstract class GroupNode<F extends ViewGroup> extends ViewNode<F> {
|
|||||||
if (child == null) {
|
if (child == null) {
|
||||||
child = ViewNode.create(getDoricContext(), type);
|
child = ViewNode.create(getDoricContext(), type);
|
||||||
child.index = i;
|
child.index = i;
|
||||||
child.mParent = this;
|
child.mSuperNode = this;
|
||||||
child.mId = id;
|
child.mId = id;
|
||||||
mChildrenNode.put(id, child);
|
mChildrenNode.put(id, child);
|
||||||
} else {
|
} else {
|
||||||
@ -110,61 +109,4 @@ public abstract class GroupNode<F extends ViewGroup> extends ViewNode<F> {
|
|||||||
super.blend(view, layoutParams, name, prop);
|
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -36,8 +36,8 @@ public class LinearNode extends GroupNode<LinearLayout> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void blendChild(ViewNode viewNode, JSObject layoutConfig) {
|
protected void blendChildLayoutConfig(ViewNode viewNode, JSObject layoutConfig) {
|
||||||
super.blendChild(viewNode, layoutConfig);
|
super.blendChildLayoutConfig(viewNode, layoutConfig);
|
||||||
JSValue jsValue = layoutConfig.getProperty("alignment");
|
JSValue jsValue = layoutConfig.getProperty("alignment");
|
||||||
if (jsValue.isNumber()) {
|
if (jsValue.isNumber()) {
|
||||||
((LinearLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt();
|
((LinearLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt();
|
||||||
|
@ -36,8 +36,8 @@ public class StackNode extends GroupNode<FrameLayout> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void blendChild(ViewNode viewNode, JSObject jsObject) {
|
protected void blendChildLayoutConfig(ViewNode viewNode, JSObject jsObject) {
|
||||||
super.blendChild(viewNode, jsObject);
|
super.blendChildLayoutConfig(viewNode, jsObject);
|
||||||
JSValue jsValue = jsObject.getProperty("alignment");
|
JSValue jsValue = jsObject.getProperty("alignment");
|
||||||
if (jsValue.isNumber()) {
|
if (jsValue.isNumber()) {
|
||||||
((FrameLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt();
|
((FrameLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt();
|
||||||
|
91
Android/doric/src/main/java/pub/doric/shader/SuperNode.java
Normal file
91
Android/doric/src/main/java/pub/doric/shader/SuperNode.java
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -42,7 +42,7 @@ import java.util.LinkedList;
|
|||||||
public abstract class ViewNode<T extends View> extends DoricContextHolder {
|
public abstract class ViewNode<T extends View> extends DoricContextHolder {
|
||||||
protected T mView;
|
protected T mView;
|
||||||
int index;
|
int index;
|
||||||
ViewNode mParent;
|
SuperNode mSuperNode;
|
||||||
String mId;
|
String mId;
|
||||||
private ViewGroup.LayoutParams mLayoutParams;
|
private ViewGroup.LayoutParams mLayoutParams;
|
||||||
|
|
||||||
@ -52,8 +52,8 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
|
|||||||
|
|
||||||
private DoricLayer doricLayer;
|
private DoricLayer doricLayer;
|
||||||
|
|
||||||
public void setParentNode(ViewNode parentNode) {
|
public void setSuperNode(SuperNode parentNode) {
|
||||||
mParent = parentNode;
|
mSuperNode = parentNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public View getDoricLayer() {
|
public View getDoricLayer() {
|
||||||
@ -128,8 +128,8 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
|
|||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "layoutConfig":
|
case "layoutConfig":
|
||||||
if (prop.isObject() && mParent instanceof GroupNode) {
|
if (prop.isObject() && mSuperNode != null) {
|
||||||
((GroupNode) mParent).blendChild(this, prop.asObject());
|
mSuperNode.blendChildLayoutConfig(this, prop.asObject());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "border":
|
case "border":
|
||||||
@ -180,7 +180,7 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
|
|||||||
ViewNode viewNode = this;
|
ViewNode viewNode = this;
|
||||||
do {
|
do {
|
||||||
ids.push(viewNode.mId);
|
ids.push(viewNode.mId);
|
||||||
viewNode = viewNode.mParent;
|
viewNode = viewNode.mSuperNode;
|
||||||
} while (viewNode != null && !(viewNode instanceof RootNode));
|
} while (viewNode != null && !(viewNode instanceof RootNode));
|
||||||
|
|
||||||
return ids.toArray(new String[0]);
|
return ids.toArray(new String[0]);
|
||||||
|
@ -51,7 +51,7 @@ public class ListAdapter extends RecyclerView.Adapter<ListAdapter.DoricViewHolde
|
|||||||
@Override
|
@Override
|
||||||
public DoricViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
public DoricViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||||
ListItemNode node = (ListItemNode) ViewNode.create(listNode.getDoricContext(), "ListItem");
|
ListItemNode node = (ListItemNode) ViewNode.create(listNode.getDoricContext(), "ListItem");
|
||||||
node.setParentNode(listNode);
|
node.setSuperNode(listNode);
|
||||||
return new DoricViewHolder(node, node.getDoricLayer());
|
return new DoricViewHolder(node, node.getDoricLayer());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +103,7 @@ public class ListAdapter extends RecyclerView.Adapter<ListAdapter.DoricViewHolde
|
|||||||
static class DoricViewHolder extends RecyclerView.ViewHolder {
|
static class DoricViewHolder extends RecyclerView.ViewHolder {
|
||||||
ListItemNode listItemNode;
|
ListItemNode listItemNode;
|
||||||
|
|
||||||
public DoricViewHolder(ListItemNode node, @NonNull View itemView) {
|
DoricViewHolder(ListItemNode node, @NonNull View itemView) {
|
||||||
super(itemView);
|
super(itemView);
|
||||||
listItemNode = node;
|
listItemNode = node;
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,7 @@ import com.github.pengfeizhou.jscore.JSValue;
|
|||||||
|
|
||||||
import pub.doric.DoricContext;
|
import pub.doric.DoricContext;
|
||||||
import pub.doric.extension.bridge.DoricPlugin;
|
import pub.doric.extension.bridge.DoricPlugin;
|
||||||
|
import pub.doric.shader.SuperNode;
|
||||||
import pub.doric.shader.ViewNode;
|
import pub.doric.shader.ViewNode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -33,7 +34,7 @@ import pub.doric.shader.ViewNode;
|
|||||||
* @CreateDate: 2019-11-12
|
* @CreateDate: 2019-11-12
|
||||||
*/
|
*/
|
||||||
@DoricPlugin(name = "List")
|
@DoricPlugin(name = "List")
|
||||||
public class ListNode extends ViewNode<RecyclerView> {
|
public class ListNode extends SuperNode<RecyclerView> {
|
||||||
private final ListAdapter listAdapter;
|
private final ListAdapter listAdapter;
|
||||||
|
|
||||||
public ListNode(DoricContext doricContext) {
|
public ListNode(DoricContext doricContext) {
|
||||||
@ -79,4 +80,9 @@ public class ListNode extends ViewNode<RecyclerView> {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void blendChildLayoutConfig(ViewNode viewNode, JSObject jsObject) {
|
||||||
|
super.blendChildLayoutConfig(viewNode, jsObject);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
@Entry
|
||||||
class ListPanel extends Panel {
|
class ListPanel extends Panel {
|
||||||
@ -9,17 +9,29 @@ class ListPanel extends Panel {
|
|||||||
heightSpec: LayoutSpec.AT_MOST,
|
heightSpec: LayoutSpec.AT_MOST,
|
||||||
}
|
}
|
||||||
rootView.addChild(list)
|
rootView.addChild(list)
|
||||||
list.itemCount = 10
|
list.itemCount = 1000
|
||||||
list.bgColor = Color.parse("#ff00ff")
|
|
||||||
list.renderItem = (idx) => {
|
list.renderItem = (idx) => {
|
||||||
const item = new ListItem
|
return listItem(text({
|
||||||
item.addChild(text({
|
layoutConfig: {
|
||||||
width: 100,
|
widthSpec: LayoutSpec.AT_MOST,
|
||||||
height: 100,
|
heightSpec: LayoutSpec.WRAP_CONTENT,
|
||||||
|
margin: {
|
||||||
|
left: 10,
|
||||||
|
right: 10,
|
||||||
|
top: 10,
|
||||||
|
bottom: 10,
|
||||||
|
},
|
||||||
|
},
|
||||||
text: `第${idx}行内容`,
|
text: `第${idx}行内容`,
|
||||||
textAlignment: gravity().center(),
|
textAlignment: gravity().center(),
|
||||||
}))
|
})).also(it => {
|
||||||
return item
|
it.gravity = gravity().center()
|
||||||
|
it.bgColor = Color.parse("#fff00f")
|
||||||
|
it.layoutConfig = {
|
||||||
|
widthSpec: LayoutSpec.AT_MOST,
|
||||||
|
heightSpec: LayoutSpec.WRAP_CONTENT,
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user