diff --git a/app/ns-ui-widgets-category/list-view/basics/article.md b/app/ns-ui-widgets-category/list-view/basics/article.md
index 3ecf95b..389aab9 100644
--- a/app/ns-ui-widgets-category/list-view/basics/article.md
+++ b/app/ns-ui-widgets-category/list-view/basics/article.md
@@ -2,11 +2,24 @@ The example demonstrates how to set a `ListView` component in XML page and how t
In the sample is shown how to define custom UI while using ListView's `itemTemplate`.
-
-
-> Note: Note, that changing the array after the list view is shown will not update the UI. You can force-update the UI using the refresh() method.
+> **Note:** The ListView's item template can contain only a single root view container.
+
+In the example above, the items source property (`myTitles`) is an array and its members are not observable objects. This means that adding or removing array member won't trigger a property change. To update the UI, you will need to explicitly refresh the listview after an item is added or removed while using the `refresh` method.
+
+
+> **Tip:** Instead of manually triggering the UI update with the help of ListView's `refresh` method, NativeScript provides the `ObservableArray`. Using an `ObservableArray` for your listview's items surce will make its members an observable objects and adding/removing an array item will automatically update the UI.
+```TypeScript
+const ObservableArray = require("tns-core-modules/data/observable-array").ObservableArray;
+// Change the items source from Array to the NativeScript's ObservableArray
+const titlesArray = new ObservableArray([
+ { title: "The Da Vinci Code" },
+ { title: "Harry Potter and the Chamber of Secrets" },
+ { title: "The Alchemist" },
+ { title: "The Godfather" },
+ { title: "Goodnight Moon" },
+ { title: "The Hobbit" }
+]);
+```
-
-> Note: When using ObservableArray the list view will be automatically updated when items are added or removed form the array.
\ No newline at end of file
diff --git a/app/ns-ui-widgets-category/list-view/basics/basics-page.js b/app/ns-ui-widgets-category/list-view/basics/basics-page.js
index 36e288d..fed6b3f 100644
--- a/app/ns-ui-widgets-category/list-view/basics/basics-page.js
+++ b/app/ns-ui-widgets-category/list-view/basics/basics-page.js
@@ -1,79 +1,42 @@
-const observableModule = require("tns-core-modules/data/observable");
-const observableArrayModule = require("tns-core-modules/data/observable-array");
-const dialogs = require("tns-core-modules/ui/dialogs");
// >> list-view-create-code
-const colors = ["red", "green", "blue"];
-const secondArray = new observableArrayModule.ObservableArray(
- [
- {
- title:"The Da Vinci Code"
- },
- {
- title:"Harry Potter and the Chamber of Secrets"
- },
- {
- title:"The Alchemist"
- },
- {
- title:"The Godfather"
- },
- {
- title:"Goodnight Moon"
- },
- {
- title:"The Hobbit"
- }
- ]);
+const Observable = require("tns-core-modules/data/observable").Observable;
+const titlesArray = [
+ { title: "The Da Vinci Code" },
+ { title: "Harry Potter and the Chamber of Secrets" },
+ { title: "The Alchemist" },
+ { title: "The Godfather" },
+ { title: "Goodnight Moon" },
+ { title: "The Hobbit" }
+];
+
function onNavigatingTo(args) {
const page = args.object;
- const vm = new observableModule.Observable();
+ const vm = new Observable();
- vm.set("myItems", colors);
- vm.set("mySecondItems", secondArray);
+ // Setting the listview binding source
+ vm.set("myTitles", titlesArray);
page.bindingContext = vm;
}
+exports.onNavigatingTo = onNavigatingTo;
+
+function onItemTap(args) {
+ const index = args.index;
+ console.log(`Second ListView item tap ${index}`);
+}
+exports.onItemTap = onItemTap;
// << list-view-create-code
+
function onTap(args) {
const page = args.object.page;
- const listView = page.getViewById("firstListView");
- // >> list-view-array
- colors.push("yellow");
- // Manually trigger the update so that the new color is shown.
- listView.refresh();
- // << list-view-array
-}
+ // >> list-view-refresh
+ const listView = page.getViewById("listView");
-function onSecondTap(args) {
- // >> list-view-observable-array
- secondArray.push(
- {
- title:"Alice's Adventures in Wonderland"
- }
- );
- // The ListView will be updated automatically.
- // << list-view-observable-array
-}
-
-function listViewItemTap(args) {
- const index = args.index;
- dialogs.alert(`ListView item tap ${index}`)
- .then(() => {
- console.log("Dialog closed!");
- });
-}
+ titlesArray.push({ title: "Game of Thrones" });
+// Manually trigger the update so that the new color is shown.
+listView.refresh();
+ // << list-view-refresh
-function secondListViewItemTap(args) {
- const index = args.index;
- dialogs.alert(`Second ListView item tap ${index}`)
- .then(() => {
- console.log("Dialog closed!");
- });
}
-
-exports.onNavigatingTo = onNavigatingTo;
exports.onTap = onTap;
-exports.onSecondTap = onSecondTap;
-exports.listViewItemTap = listViewItemTap;
-exports.secondListViewItemTap = secondListViewItemTap;
diff --git a/app/ns-ui-widgets-category/list-view/basics/basics-page.xml b/app/ns-ui-widgets-category/list-view/basics/basics-page.xml
index f17e08d..ca52c73 100644
--- a/app/ns-ui-widgets-category/list-view/basics/basics-page.xml
+++ b/app/ns-ui-widgets-category/list-view/basics/basics-page.xml
@@ -3,21 +3,18 @@
-
-
+
-
-
-
-
+
+
+
-
-
+
\ No newline at end of file
diff --git a/app/ns-ui-widgets-category/list-view/code-behind/code-behind-page.js b/app/ns-ui-widgets-category/list-view/code-behind/code-behind-page.js
index b8d3d47..bf6c7fa 100644
--- a/app/ns-ui-widgets-category/list-view/code-behind/code-behind-page.js
+++ b/app/ns-ui-widgets-category/list-view/code-behind/code-behind-page.js
@@ -1,36 +1,24 @@
// >> require-list-view
- const listViewModule = require("tns-core-modules/ui/list-view");
+const listViewModule = require("tns-core-modules/ui/list-view");
// << require-list-view
const dialogs = require("tns-core-modules/ui/dialogs");
-const labelModule = require("tns-core-modules/ui/label");
-const observableArrayModule = require("tns-core-modules/data/observable-array");
+const Label = require("tns-core-modules/ui/label").Label;
+const ObservableArray = require("tns-core-modules/data/observable-array").ObservableArray;
-const listViewArray = new observableArrayModule.ObservableArray(
- [
- {
- title:"The Da Vinci Code"
- },
- {
- title:"Harry Potter and the Chamber of Secrets"
- },
- {
- title:"The Alchemist"
- },
- {
- title:"The Godfather"
- },
- {
- title:"Goodnight Moon"
- },
- {
- title:"The Hobbit"
- }
- ]);
+const listViewArray = new ObservableArray([
+ { title: "The Da Vinci Code" },
+ { title: "Harry Potter and the Chamber of Secrets" },
+ { title: "The Alchemist" },
+ { title: "The Godfather" },
+ { title: "Goodnight Moon" },
+ { title: "The Hobbit" }
+]);
function onNavigatingTo(args) {
const page = args.object;
- const container = page.getViewById("container");
// >> create-list-view-code
+ const container = page.getViewById("container");
+
const listView = new listViewModule.ListView();
listView.className = "list-group";
listView.items = listViewArray;
@@ -38,23 +26,22 @@ function onNavigatingTo(args) {
listView.on(listViewModule.ListView.itemLoadingEvent, (args) => {
if (!args.view) {
// Create label if it is not already created.
- args.view = new labelModule.Label();
+ args.view = new Label();
args.view.className = "list-group-item";
}
(args.view).text = listViewArray.getItem(args.index).title;
});
listView.on(listViewModule.ListView.itemTapEvent, (args) => {
- // getting the index of the selected item
const tappedItemIndex = args.index;
- // getting the view of the selected ListVies's item
- // let tappedItemView = args.view;
- dialogs.alert(`Second ListView item tap ${tappedItemIndex}`)
- .then(() => {
- console.log("Dialog closed!");
- });
+ const tappedItemView = args.view;
+ dialogs.alert(`Index: ${tappedItemIndex} View: ${tappedItemView}`)
+ .then(() => {
+ console.log("Dialog closed!");
+ });
});
- // << create-list-view-code
+
container.addChild(listView);
+ // << create-list-view-code
}
exports.onNavigatingTo = onNavigatingTo;
diff --git a/app/ns-ui-widgets-category/list-view/events/article.md b/app/ns-ui-widgets-category/list-view/events/article.md
index 7f6b02c..89e6166 100644
--- a/app/ns-ui-widgets-category/list-view/events/article.md
+++ b/app/ns-ui-widgets-category/list-view/events/article.md
@@ -1,5 +1,4 @@
In the example is shown how to set up `itemTap` and `loadMoreItems` events as well as how to set up the needed callback methods in the Code-Behind.
-
\ No newline at end of file