view parent exposed in itemLoading event

This commit is contained in:
Vladimir Enchev
2015-05-26 14:11:51 +03:00
parent 433ef1496e
commit d0936dfc59
4 changed files with 50 additions and 15 deletions

View File

@ -4,6 +4,7 @@ import helper = require("../helper");
import viewModule = require("ui/core/view");
import observable = require("data/observable");
import types = require("utils/types");
import platform = require("platform");
// <snippet module="ui/list-view" title="list-view">
// # ListView
@ -128,6 +129,34 @@ export function test_set_items_to_array_loads_all_items() {
helper.buildUIAndRunTest(listView, testAction);
}
export function test_set_native_item_exposed() {
var listView = new listViewModule.ListView();
function testAction(views: Array<viewModule.View>) {
var indexes = {};
var colors = ["red", "green", "blue"];
listView.items = colors;
listView.on(listViewModule.ListView.itemLoadingEvent, function (args: listViewModule.ItemEventData) {
if (platform.device.os === platform.platformNames.ios) {
indexes[args.index] = args.ios;
} else if (platform.device.os === platform.platformNames.android) {
indexes[args.index] = args.android;
}
});
TKUnit.wait(ASYNC);
for (var item in indexes) {
if (platform.device.os === platform.platformNames.ios) {
TKUnit.assert(indexes[item] instanceof UITableViewCell, "itemLoading not called for index " + item);
} else if (platform.device.os === platform.platformNames.android) {
TKUnit.assert(indexes[item] instanceof android.view.ViewGroup, "itemLoading not called for index " + item);
}
}
};
helper.buildUIAndRunTest(listView, testAction);
}
export function test_set_items_to_array_creates_native_views() {
var listView = new listViewModule.ListView();
listView.on(listViewModule.ListView.itemLoadingEvent, loadViewWithItemNumber);

View File

@ -6,6 +6,7 @@ import stackLayout = require("ui/layouts/stack-layout");
import proxy = require("ui/core/proxy");
import dependencyObservable = require("ui/core/dependency-observable");
import color = require("color");
import definition = require("ui/list-view");
var ITEMLOADING = common.ListView.itemLoadingEvent;
var LOADMOREITEMS = common.ListView.loadMoreItemsEvent;
@ -181,13 +182,17 @@ class ListViewAdapter extends android.widget.BaseAdapter {
return true;
}
public getView(index: number, convertView: android.view.View, parent: any): android.view.View {
public getView(index: number, convertView: android.view.View, parent: android.view.ViewGroup): android.view.View {
if (!this._listView) {
return null;
}
var view = this._listView._getRealizedView(convertView, index);
var args = { eventName: ITEMLOADING, object: this._listView, index: index, view: view };
var args = <definition.ItemEventData>{
eventName: ITEMLOADING, object: this._listView, index: index, view: view,
android: parent,
ios: undefined
};
this._listView.notify(args);
if (!args.view) {

View File

@ -125,5 +125,15 @@ declare module "ui/list-view" {
* The view that is associated to the item, for which the event is raised.
*/
view: view.View;
/**
* Gets the native [iOS view](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/) that represents the user interface where the view is hosted. Valid only when running on iOS.
*/
ios: UITableViewCell;
/**
* Gets the native [android widget](http://developer.android.com/reference/android/view/ViewGroup.html) that represents the user interface where the view is hosted. Valid only when running on Android OS.
*/
android: android.view.ViewGroup;
}
}

View File

@ -19,17 +19,8 @@ require("utils/module-merge").merge(common, exports);
var infinity = utils.layout.makeMeasureSpec(0, utils.layout.UNSPECIFIED);
class ListViewCell extends UITableViewCell {
static new(): ListViewCell {
return <ListViewCell>super.new();
}
static class(): any {
return ListViewCell;
}
}
function notifyForItemAtIndex(listView: definition.ListView, cell: any, eventName: string, indexPath: NSIndexPath) {
var args = <definition.ItemEventData>{ eventName: eventName, object: listView, index: indexPath.row, view: cell.view };
var args = <definition.ItemEventData>{ eventName: eventName, object: listView, index: indexPath.row, view: cell.view, ios: cell, android: undefined };
listView.notify(args);
return args;
}
@ -54,7 +45,7 @@ class DataSource extends NSObject implements UITableViewDataSource {
public tableViewCellForRowAtIndexPath(tableView: UITableView, indexPath: NSIndexPath): UITableViewCell {
// We call this method because ...ForIndexPath calls tableViewHeightForRowAtIndexPath immediately (before we can prepare and measure it).
var cell = tableView.dequeueReusableCellWithIdentifier(CELLIDENTIFIER) || ListViewCell.new();
var cell = tableView.dequeueReusableCellWithIdentifier(CELLIDENTIFIER) || UITableViewCell.new();
this._owner._prepareCell(cell, indexPath);
var cellView: view.View = cell.view;
@ -115,7 +106,7 @@ class UITableViewDelegateImpl extends NSObject implements UITableViewDelegate {
// in iOS 7.1 this method is called before tableViewCellForRowAtIndexPath so we need fake cell to measure its content.
var cell = this._measureCell;
if (!cell) {
this._measureCell = tableView.dequeueReusableCellWithIdentifier(CELLIDENTIFIER) || ListViewCell.new();
this._measureCell = tableView.dequeueReusableCellWithIdentifier(CELLIDENTIFIER) || UITableViewCell.new();
cell = this._measureCell;
}
@ -157,7 +148,7 @@ export class ListView extends common.ListView {
super();
this._ios = new UITableView();
this._ios.registerClassForCellReuseIdentifier(ListViewCell.class(), CELLIDENTIFIER);
this._ios.registerClassForCellReuseIdentifier(UITableViewCell.class(), CELLIDENTIFIER);
this._ios.autoresizesSubviews = false;
this._ios.autoresizingMask = UIViewAutoresizing.UIViewAutoresizingNone;