【问题标题】:Adding Multiple TextView's to CollectionView in Tabrisjs在 Tabrisjs 中将多个 TextView 添加到 CollectionView
【发布时间】: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 由适合屏幕的单元格数量组成,当您上下滚动列表时,内容会根据屏幕上可见的单元格进行更新。

标签: android tabris tabris-js


【解决方案1】:

apply 方法采用 Widget 选择器,其工作方式类似于 CSS 选择器,并在上述链接中记录。您正在引用一个 JavaScript 变量,该变量不受支持且不在 updateCell 回调函数的范围内。

我会更新您的 createCell 回调,以便每个元素都有一个不同的 class 并在您的 updateCell 回调中引用它:

createCell: () => {
  let cell = new Composite();

  new TextView({
    left: 30, top: 10,
    class: 'firstName',
    alignment: 'left'
  }).appendTo(cell);

  new TextView({
    left: 50, top: 25,
    class: 'lastName',
    alignment: 'right'
  }).appendTo(cell);
  return cell;
},
updateCell: (cell, index) => {
  let person = people[index];
  cell.apply({
    '.firstName': {text: person.firstName},
    '.lastName': {text: person.lastName},
  });
}

【讨论】:

  • 工作得很好,谢谢,现在我看看如果有人点击 lastName 是否可以得到一个事件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多