|
| 1 | +import { Component, OnInit, ChangeDetectorRef } from "@angular/core"; |
| 2 | +import { ObservableArray } from "tns-core-modules/data/observable-array"; |
| 3 | +import { DataItem } from "../../dataItem"; |
| 4 | +import { ListViewLinearLayout, ListViewEventData, RadListView, ListViewLoadOnDemandMode } from "nativescript-pro-ui/listview"; |
| 5 | +import * as applicationModule from "tns-core-modules/application"; |
| 6 | +import * as Timer from "tns-core-modules/timer"; |
| 7 | +var posts = require("../../../listview/posts.json") |
| 8 | + |
| 9 | +@Component({ |
| 10 | + moduleId: module.id, |
| 11 | + selector: "tk-listview-dynamic-size-manual", |
| 12 | + templateUrl: "listview-dynamic-size-manual.component.html", |
| 13 | + styleUrls: ["listview-dynamic-size-manual.component.css"] |
| 14 | +}) |
| 15 | + |
| 16 | +export class ListViewDynamicSizeManualComponent implements OnInit { |
| 17 | + private _dataItems: ObservableArray<DataItem>; |
| 18 | + private _numberOfAddedItems; |
| 19 | + private layout: ListViewLinearLayout; |
| 20 | + |
| 21 | + constructor(private _changeDetectionRef: ChangeDetectorRef) { |
| 22 | + } |
| 23 | + |
| 24 | + ngOnInit() { |
| 25 | + this.layout = new ListViewLinearLayout(); |
| 26 | + this.layout.scrollDirection = "Vertical"; |
| 27 | + this.initDataItems(); |
| 28 | + this._changeDetectionRef.detectChanges(); |
| 29 | + } |
| 30 | + |
| 31 | + public get dataItems(): ObservableArray<DataItem> { |
| 32 | + return this._dataItems; |
| 33 | + } |
| 34 | + |
| 35 | + public onLoadMoreItemsRequested(args: ListViewEventData) { |
| 36 | + var that = new WeakRef(this); |
| 37 | + Timer.setTimeout(function () { |
| 38 | + var listView: RadListView = args.object; |
| 39 | + var initialNumberOfItems = that.get()._numberOfAddedItems; |
| 40 | + for (var i = that.get()._numberOfAddedItems; i < initialNumberOfItems + 2; i++) { |
| 41 | + if (i > posts.names.length - 1) { |
| 42 | + listView.loadOnDemandMode = ListViewLoadOnDemandMode[ListViewLoadOnDemandMode.None]; |
| 43 | + break; |
| 44 | + } |
| 45 | + |
| 46 | + var imageUri = applicationModule.android ? posts.images[i].toLowerCase() : posts.images[i]; |
| 47 | + that.get()._dataItems.push(new DataItem(i, posts.names[i], "This is item description", posts.titles[i], posts.text[i], "res://" + imageUri)); |
| 48 | + that.get()._numberOfAddedItems++; |
| 49 | + } |
| 50 | + |
| 51 | + listView.notifyLoadOnDemandFinished(); |
| 52 | + }, 1000); |
| 53 | + args.returnValue = true; |
| 54 | + } |
| 55 | + |
| 56 | + private initDataItems() { |
| 57 | + this._dataItems = new ObservableArray<DataItem>(); |
| 58 | + this._numberOfAddedItems = 0; |
| 59 | + for (var i = 0; i < posts.names.length - 15; i++) { |
| 60 | + this._numberOfAddedItems++; |
| 61 | + if (applicationModule.android) { |
| 62 | + this._dataItems.push(new DataItem(i, posts.names[i], "This is item description", posts.titles[i], posts.text[i], "res://" + posts.images[i].toLowerCase())); |
| 63 | + } |
| 64 | + else { |
| 65 | + this._dataItems.push(new DataItem(i, posts.names[i], "This is item description", posts.titles[i], posts.text[i], "res://" + posts.images[i])); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments