FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(list-view-android): app crashes on ListView item template change by ADjenkov · Pull Request #6634 · NativeScript/NativeScript · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ts  (4) .xml  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
21 changes: 21 additions & 0 deletions apps/app/ui-tests-app/list-view/dynamic-templates.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Page } from "tns-core-modules/ui/page";
import { ViewModel } from "./main-view-model";

export function pageLoaded(args) {
let page = <Page>args.object;
const viewModel = new ViewModel();

page.bindingContext = {
"items": viewModel.items
}
}

exports.onItemTap = function (args) {
const list = args.object;
let index = args.index;
let listArray = list.page.bindingContext["items"];
let currentItem = listArray.getItem(index);

currentItem.age = currentItem.age + 1;
listArray.setItem(index, currentItem);
}
14 changes: 14 additions & 0 deletions apps/app/ui-tests-app/list-view/dynamic-templates.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Page loaded="pageLoaded">
<GridLayout id="grid-layout">
<ListView id="lv1" items="{{ items }}" itemTemplateSelector="age % 2 === 0 ? 'green':'red'" itemTap="onItemTap">
<ListView.itemTemplates>
<template key="red">
<Label text="{{ name + ' ' + id }}" style.backgroundColor="red"/>
</template>
<template key="green">
<Label text="{{ name + ' ' + id }}" style.backgroundColor="green"/>
</template>
</ListView.itemTemplates>
</ListView>
</GridLayout>
</Page>
1 change: 1 addition & 0 deletions apps/app/ui-tests-app/list-view/main-page.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function loadExamples() {
const examples = new Map<string, string>();
examples.set("list-view-templates", "list-view/list-view");
examples.set("images-template", "list-view/images-template");
examples.set("dynamic-templates", "list-view/dynamic-templates");
examples.set("bindings", "list-view/listview-binding");
examples.set("listview-bg-separator-color", "list-view/listview-bg-separator-color");
examples.set("csslv", "list-view/csslv");
Expand Down
17 changes: 15 additions & 2 deletions apps/app/ui-tests-app/list-view/main-view-model.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { ObservableArray } from "tns-core-modules/data/observable-array";
export class Item extends Observable {
private _name: string;
private _id: number;
private _age: number;

constructor(name: string, id: number) {
constructor(name: string, id: number, age: number) {
super();
this._name = name;
this._id = id;
this._age = age;
}

get name(): string {
Expand All @@ -33,6 +35,17 @@ export class Item extends Observable {
}
}

get age(): number {
return this._age;
}

set age(value: number) {
if (this._age !== value) {
this._age = value;
this.notifyPropertyChange("age", value)
}
}

public toString() {
return `${this.name} ${this.id}`;
}
Expand All @@ -44,7 +57,7 @@ export class ViewModel extends Observable {
get items(): ObservableArray<Item> {
this._items = new ObservableArray<Item>();
for (let i = 0; i < 100; i++) {
this._items.push(new Item(`Item`, i));
this._items.push(new Item(`Item`, i, 0));
}
return this._items;
}
Expand Down
5 changes: 5 additions & 0 deletions tns-core-modules/ui/list-view/list-view-common.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ export abstract class ListViewBase extends ContainerView implements ListViewDefi
});
this._itemTemplateSelector = (item: any, index: number, items: any) => {
item["$index"] = index;

if (this._itemTemplateSelectorBindable.bindingContext === item) {
this._itemTemplateSelectorBindable.bindingContext = null;
}

this._itemTemplateSelectorBindable.bindingContext = item;
return this._itemTemplateSelectorBindable.get("templateKey");
};
Expand Down

Back | FazBrowse Home | New Git URL