Change detection issue in ListView
Conditional styling in ListView templates don't get consistently applied. Most times they require the item to go off screen and back in, in order for the new status to be reflected.
Gif

Here is a sample to reproduce:
Component:
import {Component} from "@angular/core";
@Component({
selector: "my-app",
templateUrl: "app.component.html",
})
export class AppComponent {
public options: string[] = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"];
public selectedOption: string = "";
public onItemTap(args) {
this.selectedOption = this.options[args.index];
}
}
Markup:
<StackLayout>
<ListView #listview [items]="options" separatorColor="white" (itemTap)="onItemTap($event)">
<template let-item="item" let-i="index" let-odd="odd" let-even="even">
<GridLayout columns="44, *" height="48">
<StackLayout colSpan="2" verticalAlignment="bottom" class="line"></StackLayout>
<Label col="1" [text]="item" style="font-size:20" [class.active]="selectedOption == item"></Label>
</GridLayout>
</template>
</ListView>
</StackLayout>
CSS
.active {
color:red;
}
well, I believe this is why I finally give up on the nativescript, a serious bug exists nearly 3 years.
I encountered the same issue. I tried to solve it by re-assigning my array to the ListView component, but once every n times my app crashes returning the following error: "TypeError: null is not an object (evaluating 'cell.view')"
Anybody knows what the source of the problem may be?
I have been feeling the same thing, my employer wanted me to try to build an app with a javascript library to test if it is a way to build other apps in the future. And when I get stuck on things like this I'm wondering if there are more holes to fall down.
I actually found a way to "solve" my issue, well it did not solve the issue but it is a way to list things without using the list view. You can see the issue here.
Is switching to RadListView a viable solution to this issue?
If you commented on my comment, I have been using radlistview from the beginning.
This is a major issue. Is there any way to get it moved off of the backlog? @NickIliev @tsonevn ?
Thank you for your consideration.
@Roar1827 or @Kraften, do you have a repository that shows this issue?
From my comment back in Dec 2016, it was able to get around this issue by using RxJS. I was about to build a little demo for you, but when I copy/pasted the code from the original post...it's working without resorting to Observables.
Here's my component, the only change I needed to make from the above code was to change <template/> to <ng-template/>: https://github.com/jzgoda/tns-listview-change-detection/tree/master/src/app
@jzgoda @Roar1827 @Kraften @Zampa98 @steve3d @larssn @ignaciofuentes @ShyshkovOleg
I might be missing something on the way, but while using the original snippet with the latest tns-core-modules and nativescript-angular and I am not experiencing this issue. Here is a Playground demo showing the case - tested on both iOS and Android (all the related code is in app.component) and on both the itemTap is triggering the change detection. Please do let me know if you guys have a different case and share a Playground (or sample app) that demonstrates the issue.
Update: the issue was also tested with RadListView and again it seems to that everything is working as expected.
Thank you for looking into this @NickIliev, I appreciate it very much.
I've tried to create a simple playground demo from extending your playground.
The problem occurs when we use [visibility] to conditionally display something when a list item is selected. Perhaps we are doing this the wrong way? Calling refresh() on the listView worked in NS 4.1 but no longer seemed to work @latest
In the demo, if you scroll down in the list view to put the item off screen, then scroll back up, the item will display correctly. When first tapping an item the height of the newly displayed item doesn't display properly.
@Roar1827 indeed you are dealing with a totally different problem which is not related to this issue or the Angular state management but to the way you are handling the state of each ListView cell. You still need to call refresh on iOS but you also need to change an individual boolean flag for each item and not a global boolean variable.
Here is a Playground demo demonstrating on how you could approach the problem with creating a boolean flag (isExpanded) for each item of your list. Then use this boolean flag to change only tapped cell (based on the passed index). Due to specifics with the measurements on iOS, you will still need to call refresh method on the listview (see the onListViewLoaded method used to get a reference for the ListView).
Closing the issue as resolved (see this comment) - if anyone still experiences a similar problem please ping me with the details needed to investigate the case (including a sample app or Playground demo)