feat:Scroller add scrollTo method

This commit is contained in:
pengfei.zhou 2020-03-03 13:10:35 +08:00 committed by osborn
parent 2927ad5679
commit 46255632ec
12 changed files with 92 additions and 11 deletions

View File

@ -21,7 +21,9 @@ import com.github.pengfeizhou.jscore.JSValue;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.DoricScrollChangeListener; import pub.doric.DoricScrollChangeListener;
import pub.doric.IDoricScrollable; import pub.doric.IDoricScrollable;
import pub.doric.extension.bridge.DoricMethod;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.utils.DoricUtils;
import pub.doric.widget.HVScrollView; import pub.doric.widget.HVScrollView;
/** /**
@ -113,4 +115,20 @@ public class ScrollerNode extends SuperNode<HVScrollView> implements IDoricScrol
public void setScrollChangeListener(DoricScrollChangeListener listener) { public void setScrollChangeListener(DoricScrollChangeListener listener) {
this.doricScrollChangeListener = listener; this.doricScrollChangeListener = listener;
} }
@DoricMethod
public void scrollTo(JSObject params) {
boolean animated = false;
if (params.getProperty("animated").isBoolean()) {
animated = params.getProperty("animated").asBoolean().value();
}
JSObject offset = params.getProperty("offset").asObject();
if (animated) {
this.mView.smoothScrollTo(DoricUtils.dp2px(offset.getProperty("x").asNumber().toFloat()),
DoricUtils.dp2px(offset.getProperty("y").asNumber().toFloat()));
} else {
this.mView.scrollTo(DoricUtils.dp2px(offset.getProperty("x").asNumber().toFloat()),
DoricUtils.dp2px(offset.getProperty("y").asNumber().toFloat()));
}
}
} }

View File

@ -1,4 +1,4 @@
import { Group, text, gravity, Color, LayoutSpec, vlayout, hlayout, layoutConfig, scroller, Text, ViewHolder, VMPanel, ViewModel, network, loge, HLayout, stack, image } from "doric"; import { Group, text, gravity, Color, LayoutSpec, vlayout, hlayout, layoutConfig, scroller, Text, ViewHolder, VMPanel, ViewModel, network, loge, HLayout, stack, image, Gravity, takeNonNull, Scroller } from "doric";
import { colors } from "./utils"; import { colors } from "./utils";
interface DoubanModel { interface DoubanModel {
@ -65,11 +65,15 @@ interface DoubanModel {
interface MovieModel { interface MovieModel {
doubanModel?: DoubanModel doubanModel?: DoubanModel
selectedIdx: number
} }
class MovieVH extends ViewHolder { class MovieVH extends ViewHolder {
title!: Text title!: Text
gallery!: HLayout gallery!: HLayout
scrolled!: Scroller
movieTitle!: Text
movieYear!: Text
build(root: Group) { build(root: Group) {
vlayout( vlayout(
@ -85,7 +89,7 @@ class MovieVH extends ViewHolder {
textAlignment: gravity().center(), textAlignment: gravity().center(),
height: 50, height: 50,
}), }),
scroller( this.scrolled = scroller(
this.gallery = hlayout( this.gallery = hlayout(
[], [],
{ {
@ -96,12 +100,26 @@ class MovieVH extends ViewHolder {
left: 20, left: 20,
right: 20, right: 20,
bottom: 20, bottom: 20,
} },
gravity: Gravity.Center,
}), }),
{ {
layoutConfig: layoutConfig().most().configHeight(LayoutSpec.FIT), layoutConfig: layoutConfig().most().configHeight(LayoutSpec.FIT),
backgroundColor: Color.parse("#eeeeee"), backgroundColor: Color.parse("#eeeeee"),
}) }),
vlayout(
[
this.movieTitle = text({
textSize: 20,
}),
this.movieYear = text({
textSize: 20,
}),
],
{
layoutConfig: layoutConfig().fit().configWidth(LayoutSpec.MOST),
gravity: Gravity.Center
}),
], ],
{ {
layoutConfig: layoutConfig().most(), layoutConfig: layoutConfig().most(),
@ -122,20 +140,27 @@ class MovieVM extends ViewModel<MovieModel, MovieVH>{
if (state.doubanModel) { if (state.doubanModel) {
vh.title.text = state.doubanModel.title vh.title.text = state.doubanModel.title
vh.gallery.children.length = 0 vh.gallery.children.length = 0
state.doubanModel.subjects.slice(0, 5).forEach(e => { state.doubanModel.subjects.slice(0, 5).forEach((e, idx) => {
vh.gallery.addChild(stack( vh.gallery.addChild(stack(
[ [
image({ image({
layoutConfig: layoutConfig().just(), layoutConfig: layoutConfig().just(),
width: 270 / 2, width: 270 / 2 * (idx == state.selectedIdx ? 1.2 : 1),
height: 400 / 2, height: 400 / 2 * (idx == state.selectedIdx ? 1.2 : 1),
imageUrl: e.images.large imageUrl: e.images.large,
onClick: () => {
this.updateState(state => state.selectedIdx = idx)
vh.scrolled.scrollTo(context, { x: idx * 200, y: 0 })
},
}) })
], ],
{ {
backgroundColor: Color.YELLOW,
})) }))
}) })
takeNonNull(state.doubanModel.subjects[state.selectedIdx])(it => {
vh.movieTitle.text = it.title
vh.movieYear.text = it.year
})
} }
} }
} }
@ -150,7 +175,7 @@ class SliderPanel extends VMPanel<MovieModel, MovieVH>{
} }
getState() { getState() {
return {} return { selectedIdx: 0 }
} }
} }

View File

@ -22,6 +22,7 @@
#import "DoricScrollerNode.h" #import "DoricScrollerNode.h"
#import "DoricExtensions.h" #import "DoricExtensions.h"
#import "DoricRefreshableNode.h" #import "DoricRefreshableNode.h"
#import "DoricPromise.h"
@implementation DoricScrollView @implementation DoricScrollView
@ -132,4 +133,11 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
self.didScrollListener(scrollView); self.didScrollListener(scrollView);
} }
} }
- (void)scrollTo:(NSDictionary *)params {
BOOL animated = [params[@"animated"] boolValue];
NSDictionary *offsetDic = params[@"offset"];
CGPoint offset = CGPointMake([offsetDic[@"x"] floatValue], [offsetDic[@"y"] floatValue]);
[self.view setContentOffset:offset animated:animated];
}
@end @end

View File

@ -2004,6 +2004,9 @@ var Scroller = /** @class */ (function (_super) {
this.dirtyProps.content = this.content.viewId; this.dirtyProps.content = this.content.viewId;
return _super.prototype.toModel.call(this); return _super.prototype.toModel.call(this);
}; };
Scroller.prototype.scrollTo = function (context, offset, animated) {
return this.nativeChannel(context, "scrollTo")({ offset: offset, animated: animated });
};
return Scroller; return Scroller;
}(Superview)); }(Superview));

View File

@ -1499,6 +1499,9 @@ class Scroller extends Superview {
this.dirtyProps.content = this.content.viewId; this.dirtyProps.content = this.content.viewId;
return super.toModel(); return super.toModel();
} }
scrollTo(context, offset, animated) {
return this.nativeChannel(context, "scrollTo")({ offset, animated });
}
} }
var __decorate$7 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) { var __decorate$7 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {

View File

@ -2958,6 +2958,9 @@ class Scroller extends Superview {
this.dirtyProps.content = this.content.viewId; this.dirtyProps.content = this.content.viewId;
return super.toModel(); return super.toModel();
} }
scrollTo(context, offset, animated) {
return this.nativeChannel(context, "scrollTo")({ offset, animated });
}
} }
var __decorate$7 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) { var __decorate$7 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {

5
doric-js/index.d.ts vendored
View File

@ -570,6 +570,7 @@ declare module 'doric/lib/src/widget/slider' {
declare module 'doric/lib/src/widget/scroller' { declare module 'doric/lib/src/widget/scroller' {
import { Superview, View, IView, NativeViewModel } from 'doric/lib/src/ui/view'; import { Superview, View, IView, NativeViewModel } from 'doric/lib/src/ui/view';
import { BridgeContext } from 'doric/lib/src/runtime/global';
export function scroller(content: View, config?: IScroller): Scroller; export function scroller(content: View, config?: IScroller): Scroller;
export interface IScroller extends IView { export interface IScroller extends IView {
content?: View; content?: View;
@ -578,6 +579,10 @@ declare module 'doric/lib/src/widget/scroller' {
content: View; content: View;
allSubviews(): View[]; allSubviews(): View[];
toModel(): NativeViewModel; toModel(): NativeViewModel;
scrollTo(context: BridgeContext, offset: {
x: number;
y: number;
}, animated?: boolean): Promise<any>;
} }
} }

View File

@ -1,4 +1,5 @@
import { Superview, View, IView, NativeViewModel } from '../ui/view'; import { Superview, View, IView, NativeViewModel } from '../ui/view';
import { BridgeContext } from '../runtime/global';
export declare function scroller(content: View, config?: IScroller): Scroller; export declare function scroller(content: View, config?: IScroller): Scroller;
export interface IScroller extends IView { export interface IScroller extends IView {
content?: View; content?: View;
@ -7,4 +8,8 @@ export declare class Scroller extends Superview implements IScroller {
content: View; content: View;
allSubviews(): View[]; allSubviews(): View[];
toModel(): NativeViewModel; toModel(): NativeViewModel;
scrollTo(context: BridgeContext, offset: {
x: number;
y: number;
}, animated?: boolean): Promise<any>;
} }

View File

@ -34,4 +34,7 @@ export class Scroller extends Superview {
this.dirtyProps.content = this.content.viewId; this.dirtyProps.content = this.content.viewId;
return super.toModel(); return super.toModel();
} }
scrollTo(context, offset, animated) {
return this.nativeChannel(context, "scrollTo")({ offset, animated });
}
} }

View File

@ -15,6 +15,7 @@
*/ */
import { Superview, View, IView, NativeViewModel } from '../ui/view' import { Superview, View, IView, NativeViewModel } from '../ui/view'
import { layoutConfig } from '../util/layoutconfig' import { layoutConfig } from '../util/layoutconfig'
import { BridgeContext } from '../runtime/global'
export function scroller(content: View, config?: IScroller) { export function scroller(content: View, config?: IScroller) {
return (new Scroller).also(v => { return (new Scroller).also(v => {
@ -43,4 +44,8 @@ export class Scroller extends Superview implements IScroller {
this.dirtyProps.content = this.content.viewId this.dirtyProps.content = this.content.viewId
return super.toModel() return super.toModel()
} }
scrollTo(context: BridgeContext, offset: { x: number, y: number }, animated?: boolean) {
return this.nativeChannel(context, "scrollTo")({ offset, animated })
}
} }

View File

@ -3016,6 +3016,9 @@ class Scroller extends Superview {
this.dirtyProps.content = this.content.viewId; this.dirtyProps.content = this.content.viewId;
return super.toModel(); return super.toModel();
} }
scrollTo(context, offset, animated) {
return this.nativeChannel(context, "scrollTo")({ offset, animated });
}
} }
var __decorate$7 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) { var __decorate$7 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {

File diff suppressed because one or more lines are too long