Android上的钛合金listview响应定向

Android上的钛合金listview响应定向,android,listview,titanium,Android,Listview,Titanium,我正在编写一个应用程序,它将运行在Android和iOS平板电脑上,支持两种平台上的多种屏幕大小,以及横向和纵向 该应用程序使用ListView,我想根据平板电脑的方向对ListView行中的内容进行更改 使用横向,ListView行应该包含彼此相邻的文本和图形。纵向,图形应显示在文本下方 通过创建两个包含不同ListView的视图,并根据方向显示相应的视图,我能够在iOS上实现这一点。不幸的是,我无法在Android上复制这些结果。我在应用程序的其他区域使用了类似的“按方向查看”方法,这两个平

我正在编写一个应用程序,它将运行在Android和iOS平板电脑上,支持两种平台上的多种屏幕大小,以及横向和纵向

该应用程序使用ListView,我想根据平板电脑的方向对ListView行中的内容进行更改

使用横向,ListView行应该包含彼此相邻的文本和图形。纵向,图形应显示在文本下方

通过创建两个包含不同ListView的视图,并根据方向显示相应的视图,我能够在iOS上实现这一点。不幸的是,我无法在Android上复制这些结果。我在应用程序的其他区域使用了类似的“按方向查看”方法,这两个平台都支持该方法,因此似乎是ListView造成了问题

我在下面提供了一个小的演示应用程序,它反映了这些症状:肖像上的文本颜色为绿色,风景上的文本颜色为红色,为了简单起见,没有图像:

var win = Ti.UI.createWindow({backgroundColor: 'white'});

var viewPortrait = Ti.UI.createView({
  top: '20%',
  bottom: '20%',
  left: '10%',
  right: '10%'
});
var templatePortrait = {
  childTemplates: [{ 
    type: 'Ti.UI.Label', 
    bindId: 'info',
    properties: {
     color: 'green',
     left: '5%', width: '200dip', height: '200dip',
     textAlign: 'right'
    },
  }]
};
var listViewPortrait = Ti.UI.createListView({
  templates: { 'templatep': templatePortrait },
  defaultItemTemplate: 'templatep'
});

var viewLandscape = Ti.UI.createView({
  top: '20%',
  bottom: '20%',
  left: '10%',
  right: '10%'
});
var templateLandscape = {
  childTemplates: [{ 
    type: 'Ti.UI.Label', 
    bindId: 'info',
    properties: {
      color: 'red',
      left: '5%', width: '200dip', height: '200dip',
      textAlign: 'right' 
    }
  }]
};
var listViewLandscape = Ti.UI.createListView({
  templates: { 'templatel': templateLandscape },
  defaultItemTemplate: 'templatel'
});

var sections = [];
var section = Ti.UI.createListSection();
var data = [
  { properties:{height: '160dip'}, info: {text: 'Line text number 1'}},
  { properties:{height: '160dip'}, info: {text: 'Line text number 2'}},
  { properties:{height: '160dip'}, info: {text: 'Line text number 3'}},
];
section.setItems(data);
sections.push(section);

listViewPortrait.setSections(sections);
viewPortrait.add(listViewPortrait);

listViewLandscape.setSections(sections);
viewLandscape.add(listViewLandscape);

win.add(viewPortrait);
win.add(viewLandscape);
win.open();

Ti.Gesture.addEventListener('orientationchange', function () { 
  if (Ti.Gesture.isLandscape() === true) {
    viewPortrait.hide();
    viewLandscape.show();
  } else {
    viewPortrait.show();
    viewLandscape.hide();
  }
}); 
在Android上,文本颜色永远不会变为红色。当我在orientationchange侦听器中注释掉viewscarland.show时,ListView会完全消失,因为viewtrait.hide也证明了侦听器正在运行

我知道显示viewtrait和viewscanner的初始视图并不理想,但这只是一个测试&我希望随着方向的改变,orientationchange事件监听器会对此进行清理。它似乎像我在iOS上期望的那样工作

我有两个问题

为什么.show方法不能像我在带有ListView的Android上预期的那样工作

有没有更好的方法来处理总体方向的变化?我已经阅读了Titanium的“响应方向更改”部分,但不清楚如何触发一个事件,在加载模板后实际更改ListView。显然,创建ListView后无法更改模板,并且setTemplate方法不存在


谢谢你的帮助

我通过使用多个模板解决了这个问题,不管怎样,这都是一个更干净的代码

var listView = Ti.UI.createListView({
  templates: { 
    'templatePortrait': templatePortrait,
    'templateLandscape': templateLandscape
  },
  defaultItemTemplate: 'templatePortrait'
});
。。。。剪断

Ti.Gesture.addEventListener('orientationchange', function () { 
    if (Ti.Gesture.isLandscape() === true) {
        // Landscape
        listView.defaultItemTemplate = 'templateLandscape';
    } else {
        // Portrait
        listView.defaultItemTemplate = 'templatePortrait';
    }
}); 

您可以尝试view.visible=true和view.visible=false而不是show和hide方法。我尝试使用setVisible方法而不是show&hide,结果相同。事实上,在我的测试中,无论visible属性设置为什么,添加到窗口的第一个视图都会出现在Android上。此行为仅在视图中使用ListView时存在。