问题
I am creating a dynamic GridLayout in Nativescript following the example on there page:
https://docs.nativescript.org/ui/layouts/layout-containers
It allows my to make a GridLayout using javascript, as I need it to be dynamic.
However, I want to put this layout in a ScrollView.
I believe I'm meant to do something like this
const grid = new GridLayout();
const scroll = new ScrollView();
grid.addChild();
...
scroll.addView(grid);
I constantly get an error saying "addView" is not a function.
Is this the correct method? What 'require("");' do I need for the ScrollView?
Thanks
回答1:
You need to import from
import { ScrollView } from 'ui/scroll-view';
and it's not addView, available public method is _addView
回答2:
So just as reference for anyone else in the future.
The _addView() method wouldn't throw any errors, however I couldn't get the scene to actually load or show anything.
If there is a way to load the _addView() correctly then more than happy fo someone to add that comment.
The playground for that method is below:
https://play.nativescript.org/?template=play-js&id=LlsHLd
The way I got it working is by doing the following:
const scrollView = require("tns-core-modules/ui/scroll-view").Scroll-View;
var scrollV = new scrollView();
(Dynamically load a gridlayout)
grid.addChild()
....
scroll.content = grid;
page.content = scroll;
Thanks everyone for the help.
来源:https://stackoverflow.com/questions/52825771/nativescript-code-behind-add-gridlayout-to-scrollview-dynamically