add scrollview for android

This commit is contained in:
pengfei.zhou 2019-11-18 20:09:36 +08:00
parent 02bb3837b4
commit b9baf938f4
11 changed files with 2501 additions and 8 deletions

View File

@ -50,7 +50,7 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
doricContext = DoricContext.create(this, DoricUtils.readAssetFile("demo/ListDemo.js"), "test"); doricContext = DoricContext.create(this, DoricUtils.readAssetFile("demo/ScrollerDemo.js"), "test");
doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
doricContext.getRootNode().setRootView((FrameLayout) findViewById(R.id.root)); doricContext.getRootNode().setRootView((FrameLayout) findViewById(R.id.root));

View File

@ -20,6 +20,7 @@ import android.text.TextUtils;
import pub.doric.plugin.ShaderPlugin; import pub.doric.plugin.ShaderPlugin;
import pub.doric.shader.HLayoutNode; import pub.doric.shader.HLayoutNode;
import pub.doric.shader.ImageNode; import pub.doric.shader.ImageNode;
import pub.doric.shader.ScrollerNode;
import pub.doric.shader.list.ListItemNode; import pub.doric.shader.list.ListItemNode;
import pub.doric.shader.list.ListNode; import pub.doric.shader.list.ListNode;
import pub.doric.shader.RootNode; import pub.doric.shader.RootNode;
@ -70,6 +71,7 @@ public class DoricRegistry {
this.registerViewNode(HLayoutNode.class); this.registerViewNode(HLayoutNode.class);
this.registerViewNode(ListNode.class); this.registerViewNode(ListNode.class);
this.registerViewNode(ListItemNode.class); this.registerViewNode(ListItemNode.class);
this.registerViewNode(ScrollerNode.class);
initRegistry(this); initRegistry(this);
} }

View File

@ -34,8 +34,6 @@ public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
private ArrayList<ViewNode> mChildNodes = new ArrayList<>(); private ArrayList<ViewNode> mChildNodes = new ArrayList<>();
private ArrayList<String> mChildViewIds = new ArrayList<>(); private ArrayList<String> mChildViewIds = new ArrayList<>();
protected boolean mReusable = false;
public GroupNode(DoricContext doricContext) { public GroupNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
} }
@ -80,8 +78,8 @@ public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
mView.removeView(oldNode.getDoricLayer()); mView.removeView(oldNode.getDoricLayer());
ViewNode newNode = ViewNode.create(getDoricContext(), type); ViewNode newNode = ViewNode.create(getDoricContext(), type);
newNode.setId(id); newNode.setId(id);
if (newNode instanceof GroupNode) { if (newNode instanceof SuperNode) {
((GroupNode) newNode).mReusable = this.mReusable; ((SuperNode) newNode).mReusable = this.mReusable;
} }
newNode.init(this); newNode.init(this);
newNode.blend(model.getProperty("props").asObject()); newNode.blend(model.getProperty("props").asObject());
@ -126,8 +124,8 @@ public abstract class GroupNode<F extends ViewGroup> extends SuperNode<F> {
//Insert //Insert
ViewNode newNode = ViewNode.create(getDoricContext(), type); ViewNode newNode = ViewNode.create(getDoricContext(), type);
newNode.setId(id); newNode.setId(id);
if (newNode instanceof GroupNode) { if (newNode instanceof SuperNode) {
((GroupNode) newNode).mReusable = this.mReusable; ((SuperNode) newNode).mReusable = this.mReusable;
} }
newNode.init(this); newNode.init(this);
newNode.blend(model.getProperty("props").asObject()); newNode.blend(model.getProperty("props").asObject());

View File

@ -0,0 +1,101 @@
/*
* 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 com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue;
import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.widget.HVScrollView;
/**
* @Description: pub.doric.shader
* @Author: pengfei.zhou
* @CreateDate: 2019-11-18
*/
@DoricPlugin(name = "Scroller")
public class ScrollerNode extends SuperNode<HVScrollView> {
private String mChildViewId;
private ViewNode mChildNode;
public ScrollerNode(DoricContext doricContext) {
super(doricContext);
}
@Override
public ViewNode getSubNodeById(String id) {
return id.equals(mChildNode.getId()) ? mChildNode : null;
}
@Override
protected void blendSubNode(JSObject subProperties) {
if (mChildNode != null) {
mChildNode.blend(subProperties);
}
}
@Override
protected HVScrollView build() {
return new HVScrollView(getContext());
}
@Override
protected void blend(HVScrollView view, String name, JSValue prop) {
if ("content".equals(name)) {
mChildViewId = prop.asString().value();
} else {
super.blend(view, name, prop);
}
}
@Override
public void blend(JSObject jsObject) {
super.blend(jsObject);
JSObject contentModel = getSubModel(mChildViewId);
if (contentModel == null) {
return;
}
String viewId = contentModel.getProperty("id").asString().value();
String type = contentModel.getProperty("type").asString().value();
JSObject props = contentModel.getProperty("props").asObject();
if (mChildNode != null) {
if (mChildNode.getId().equals(viewId)) {
//skip
} else {
if (mReusable && type.equals(mChildNode.getType())) {
mChildNode.setId(viewId);
mChildNode.blend(props);
} else {
mView.removeAllViews();
mChildNode = ViewNode.create(getDoricContext(), type);
mChildNode.setId(viewId);
mChildNode.init(this);
mChildNode.blend(props);
mView.addView(mChildNode.getDoricLayer());
}
}
} else {
mChildNode = ViewNode.create(getDoricContext(), type);
mChildNode.setId(viewId);
mChildNode.init(this);
mChildNode.blend(props);
mView.addView(mChildNode.getDoricLayer());
}
}
}

View File

@ -35,6 +35,7 @@ import pub.doric.utils.DoricUtils;
*/ */
public abstract class SuperNode<V extends View> extends ViewNode<V> { public abstract class SuperNode<V extends View> extends ViewNode<V> {
private Map<String, JSObject> subNodes = new HashMap<>(); private Map<String, JSObject> subNodes = new HashMap<>();
protected boolean mReusable = false;
public SuperNode(DoricContext doricContext) { public SuperNode(DoricContext doricContext) {
super(doricContext); super(doricContext);

View File

@ -37,7 +37,9 @@ public class TextNode extends ViewNode<TextView> {
@Override @Override
protected TextView build() { protected TextView build() {
return new TextView(getContext()); TextView tv = new TextView(getContext());
tv.setGravity(Gravity.CENTER);
return tv;
} }
@Override @Override

File diff suppressed because it is too large Load Diff

View File

@ -2,4 +2,5 @@ export default [
'src/Counter', 'src/Counter',
'src/Snake', 'src/Snake',
'src/ListDemo', 'src/ListDemo',
'src/ScrollerDemo',
] ]

60
demo/src/ScrollerDemo.ts Normal file
View File

@ -0,0 +1,60 @@
import { Group, Panel, List, text, gravity, Color, Stack, LayoutSpec, list, NativeCall, listItem, log, vlayout, Gravity, hlayout, scroller } from "doric";
const colors = [
"#f0932b",
"#eb4d4b",
"#6ab04c",
"#e056fd",
"#686de0",
"#30336b",
]
@Entry
class ScrollerPanel extends Panel {
build(rootView: Group): void {
rootView.addChild(scroller(vlayout(
[
// ...[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5].map(e => text({
// text: colors[e % colors.length],
// textColor: Color.parse('#ffffff'),
// textSize: 20,
// bgColor: Color.parse(colors[e % colors.length]),
// layoutConfig: {
// widthSpec: LayoutSpec.EXACTLY,
// heightSpec: LayoutSpec.EXACTLY,
// },
// width: 200,
// height: 50,
// })),
...[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5].map(i => hlayout([
...[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5].map(j => text({
text: colors[(i + j) % colors.length],
textColor: Color.parse('#ffffff'),
textSize: 20,
bgColor: Color.parse(colors[(i + j) % colors.length]),
layoutConfig: {
widthSpec: LayoutSpec.EXACTLY,
heightSpec: LayoutSpec.EXACTLY,
},
width: 80,
height: 50,
})),
]
)),
hlayout([
...[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5].map(e => text({
text: colors[e % colors.length],
textColor: Color.parse('#ffffff'),
textSize: 20,
bgColor: Color.parse(colors[e % colors.length]),
layoutConfig: {
widthSpec: LayoutSpec.EXACTLY,
heightSpec: LayoutSpec.EXACTLY,
},
width: 200,
height: 50,
})),
]
),
]
)))
}
}

View File

@ -16,6 +16,7 @@
export * from "./src/ui/view" export * from "./src/ui/view"
export * from "./src/ui/layout" export * from "./src/ui/layout"
export * from "./src/ui/listview" export * from "./src/ui/listview"
export * from "./src/ui/scroller"
export * from "./src/ui/widgets" export * from "./src/ui/widgets"
export * from "./src/ui/panel" export * from "./src/ui/panel"
export * from "./src/ui/declarative" export * from "./src/ui/declarative"

View File

@ -0,0 +1,43 @@
/*
* 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.
*/
import { Superview, View, Property, IView, LayoutSpec } from './view'
export function scroller(content: View) {
return (new Scroller).also(v => {
v.layoutConfig = {
widthSpec: LayoutSpec.WRAP_CONTENT,
heightSpec: LayoutSpec.WRAP_CONTENT,
}
v.content = content
})
}
export interface IScroller extends IView {
content: View
}
export class Scroller extends Superview implements IScroller {
content!: View
allSubviews() {
return [this.content]
}
toModel() {
this.dirtyProps.content = this.content.viewId
return super.toModel()
}
}