@@ -19,13 +19,12 @@ import {
1919 Host ,
2020 ChangeDetectionStrategy
2121} from "@angular/core" ;
22- import { isBlank } from "../lang-facade" ;
2322import { isListLikeIterable } from "../collection-facade" ;
24- import { ListView } from "tns-core-modules/ui/list-view" ;
23+ import { ListView , ItemEventData } from "tns-core-modules/ui/list-view" ;
2524import { View , KeyedTemplate } from "tns-core-modules/ui/core/view" ;
2625import { ObservableArray } from "tns-core-modules/data/observable-array" ;
2726import { LayoutBase } from "tns-core-modules/ui/layouts/layout-base" ;
28- import { listViewLog } from "../trace" ;
27+ import { listViewLog , listViewError } from "../trace" ;
2928
3029const NG_VIEW = "_ngViewRef" ;
3130
@@ -147,27 +146,31 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit {
147146 this . _templateMap . set ( key , keyedTemplate ) ;
148147 }
149148
150- public onItemLoading ( args ) {
149+ public onItemLoading ( args : ItemEventData ) {
151150 if ( ! args . view && ! this . itemTemplate ) {
152151 return ;
153152 }
154153
155- let index = args . index ;
156- let items = args . object . items ;
157- let currentItem = typeof ( items . getItem ) === "function" ?
158- items . getItem ( index ) : items [ index ] ;
154+ const index = args . index ;
155+ const items = ( < any > args . object ) . items ;
156+ const currentItem = typeof items . getItem === "function" ? items . getItem ( index ) : items [ index ] ;
159157 let viewRef : EmbeddedViewRef < ListItemContext > ;
160158
161- if ( args . view && args . view [ NG_VIEW ] ) {
159+ if ( args . view ) {
162160 listViewLog ( "onItemLoading: " + index + " - Reusing existing view" ) ;
163161 viewRef = args . view [ NG_VIEW ] ;
164- // getting angular view from original element (in cases when ProxyViewContainer
162+ // Getting angular view from original element (in cases when ProxyViewContainer
165163 // is used NativeScript internally wraps it in a StackLayout)
164+ if ( ! viewRef && args . view instanceof LayoutBase && args . view . getChildrenCount ( ) > 0 ) {
165+ viewRef = args . view . getChildAt ( 0 ) [ NG_VIEW ] ;
166+ }
167+
166168 if ( ! viewRef ) {
167- viewRef = ( args . view . _subViews && args . view . _subViews . length > 0 ) ?
168- args . view . _subViews [ 0 ] [ NG_VIEW ] : undefined ;
169+ listViewError ( "ViewReference not found for item " + index + ". View recycling is not working" ) ;
169170 }
170- } else {
171+ } ;
172+
173+ if ( ! viewRef ) {
171174 listViewLog ( "onItemLoading: " + index + " - Creating view from template" ) ;
172175 viewRef = this . loader . createEmbeddedView ( this . itemTemplate , new ListItemContext ( ) , 0 ) ;
173176 args . view = getItemViewRoot ( viewRef ) ;
@@ -180,9 +183,6 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit {
180183 }
181184
182185 public setupViewRef ( viewRef : EmbeddedViewRef < ListItemContext > , data : any , index : number ) : void {
183- if ( isBlank ( viewRef ) ) {
184- return ;
185- }
186186 const context = viewRef . context ;
187187 context . $implicit = data ;
188188 context . item = data ;
@@ -194,13 +194,9 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit {
194194 }
195195
196196 private detectChangesOnChild ( viewRef : EmbeddedViewRef < ListItemContext > , index : number ) {
197- // Manually detect changes in child view ref
198- // TODO: Is there a better way of getting viewRef"s change detector
199- const childChangeDetector = < ChangeDetectorRef > ( < any > viewRef ) ;
200-
201197 listViewLog ( "Manually detect changes in child: " + index ) ;
202- childChangeDetector . markForCheck ( ) ;
203- childChangeDetector . detectChanges ( ) ;
198+ viewRef . markForCheck ( ) ;
199+ viewRef . detectChanges ( ) ;
204200 }
205201
206202 ngDoCheck ( ) {
0 commit comments