Your awesome NativeScript plugin.
A NativeScript CollectionView widget. The CollectionView displays data in separate cells, each cell representing one data item. For iOS wraps up UICollectionView and for Android wraps up RecyclerView
tns plugin add nativescript-collectionview
This command automatically installs the necessary files, as well as stores nativescript-collectionview as a dependency in your project's package.json file.
* itemTap
Triggered when the user taps on an item in the CollectionView.
* loadMoreItems
Triggered when the generated items reached the end of the items property.
itemTapEvent - String*
String value used when hooking to itemTapEvent event.
loadMoreItemsEvent - String*
String value used when hooking to itemTapEvent event.
android - android.support.v7.widget.RecyclerView*
Gets the native android widget that represents the user interface for this component. Valid only when running on Android OS.
items - Array | ItemsSource*
Gets or sets the items collection of the CollectionView. The items property can be set to an array or an object defining length and getItem(index) method.
itemTemplate - String*
Gets or sets the item template of the CollectionView.
rowHeight - PercentLength*
Gets or sets the height for every row in the CollectionView.
colWidth - PercentLength*
Gets or sets the width for every column in the CollectionView.
* scrollToIndex(index: number, animated: boolean = true)
Scrolls the CollectionView to the item with the given index. This can be either animated or not. Defaults to animated.
xmlns:gv="nativescript-collectionview" to your page tag, and then simply use in order to add the widget to your page. Use to specify the template for each cell:``xml`
`ts
// test-page.ts
import { EventData, Observable } from "data/observable";
import { ObservableArray } from "data/observable-array";
import { Page } from "ui/page";
import { CollectionViewItemEventData } from "nativescript-collectionview";
let viewModel: Observable;
export function pageLoaded(args: EventData) {
const page = args.object as Page;
const items = new ObservableArray();
for (let loop = 0; loop < 200; loop++) {
items.push({ value: "test " + loop.toString() });
}
viewModel = new Observable();
viewModel.set("items", items);
page.bindingContext = viewModel;
}
export function gridViewItemTap(args: CollectionViewItemEventData) {
console.log("tap index " + args.index.toString());
}
export function gridViewItemLoading(args: CollectionViewItemEventData) {
console.log("item loading " + args.index.toString());
}
export function gridViewLoadMoreItems(args: EventData) {
console.log("load more items");
}
`
You can also have multiple templates the same way you add them in the builtin ListView control:`xml
itemTap="gridViewItemTap" itemLoading="gridViewItemLoading" loadMoreItems="gridViewLoadMoreItems">
``ts`
export function templateSelector(item: any, index: number, items: any) {
return index % 2 === 0 ? "even" : "odd";
}
Import CollectionViewModule in your NgModule:
`typescript
import { CollectionViewModule } from 'nativescript-collectionview/angular';
@NgModule({
//......
imports: [
//......
CollectionViewModule,
//......
],
//......
})
`
#### Example Usage
`ts
// app.module.ts
import { CollectionViewModule } from "nativescript-collectionview/angular";
@NgModule({
bootstrap: [
AppComponent
],
imports: [
NativeScriptModule,
AppRoutingModule,
CollectionViewModule,
],
declarations: [
AppComponent,
ItemsComponent,
ItemDetailComponent
],
providers: [
ItemService
],
schemas: [
NO_ERRORS_SCHEMA
]
})
export class AppModule { }
`
`html`
If you want to use multiple item templates, you can do that very similarly to how you do it for the builtin ListView control. The only difference is that due to current limitations instead of using the nsTemplateKey directive you need to use the cvTemplateKey directive that comes from the CollectionView. (In a future version, once the framework allows it this will be changed and you will be able to use the same directive for the CollectionView as well)`html
`
file:
`js
var gridViewMangleExcludes = require("nativescript-collectionview/uglify-mangle-excludes").default;
//......
module.exports = function (platform, destinationApp) {
//......
if (process.env.npm_config_uglify) {
plugins.push(new webpack.LoaderOptionsPlugin({
minimize: true
})); //Work around an Android issue by setting compress = false
var compress = platform !== "android";
plugins.push(new webpack.optimize.UglifyJsPlugin({
mangle: {
except: nsWebpack.uglifyMangleExcludes.concat(gridViewMangleExcludes),
},
compress: compress,
}));
}
//......
}
`Demos
This repository includes both Angular and plain NativeScript demos. In order to run those execute the following in your shell:
`shell
$ git clone https://github.com/peterstaev/nativescript-collectionview
$ cd nativescript-collectionview
$ npm install
$ npm run demo-ios
`
This will run the plain NativeScript demo project on iOS. If you want to run it on Android simply use the -android instead of the -ios sufix. If you want to run the Angular demo simply use the
demo-ng- prefix instead of demo-. Donate
bitcoin:14fjysmpwLvSsAskvLASw6ek5XfhTzskHC`