【发布时间】:2018-12-05 10:48:53
【问题描述】:
我可以轻松地将小部件添加到 CollectionView,但我似乎无法添加多个小部件类型。我正在尝试添加 2 个 TextView。这是我到目前为止得到的结果,它仅输出了 firstName 两次。 (这在 Playground 中运行)
另外,是否可以向每个 TextView 添加事件?喜欢:
.on('tap', () => {
我确实看到 .on('select',在 Collection View 上的工作方式,但我想将事件添加到每个 TextView 的
谢谢。
// Create a collection view, initialize its cells and fill it with items
const {CollectionView, Composite, ImageView, TextView, ui} = require('tabris');
let people = [
['Bob', 'Smith',],
['Fred', 'Jones'],
['James', 'Mackay'],
].map(([firstName, lastName]) => ({firstName, lastName}));
new CollectionView({
left: 0, top: 0, right: 0, bottom: 0,
itemCount: people.length,
cellHeight: 56,
createCell: () => {
let cell = new Composite();
new TextView({
left: 30, top: 10,
alignment: 'left'
})
.appendTo(cell);
let txvLastName = new TextView({
left: 50, top: 25,
alignment: 'right'
})
.appendTo(cell);
return cell;
},
updateCell: (cell, index) => {
let person = people[index];
cell.apply({
TextView: {text: person.firstName},
txvLastName: {text: person.lastName},
});
}
}).on('select', ({index}) => console.log('selected', people[index].firstName))
.appendTo(ui.contentView);
【问题讨论】:
-
我看到 GitHub 已关闭票证 github.com/eclipsesource/tabris-js/issues/1646 - CollectionView 的子项不是有效的返回值,不应使用。由于您正在重用 CV 中的单元格,因此您无法访问 updateCells 方法之外的小部件。 所以,我认为我无法在 CV 内的小部件上获取事件。
-
您可以,但请记住,您不是在对 CollectionView 本身的子项进行操作。 CollectionView 由适合屏幕的单元格数量组成,当您上下滚动列表时,内容会根据屏幕上可见的单元格进行更新。