2015-03-03 83 views
0

我正在设计一个视图,使用Android Appcelerator for Android/ios.I需要设计一个时间选择器(水平)。我设计了水平视图并在其中输入了值。 enter image description hereAndroid/ios:Titanium Appcelerator水平滚动

但不知道如何挑选完美的箭头下的值。以及最后一个数字如何居中(箭头下方)。 请指导..

CODE:

var win1 = Titanium.UI.createWindow({ 
    title:'Tab 1', 
    backgroundColor:'#fff'}); 
var scrollAlbums = Ti.UI.createScrollView({ 
    bottom: 10, 
    contentHeight: Ti.UI.SIZE, 
    contentWidth: Ti.UI.SIZE, 
    height: 95, 
    layout: 'horizontal', 
    showHorizontalScrollIndicator: false, 
    showVerticalScrollIndicator: true, 
    scrollType: 'horizontal', 
    horizontalWrap: false, 
    width: Ti.UI.FILL 
}); 
var data=[]; 
var circle=[]; 
for(var i=0;i<20;i++){ 
    circle[i] = Titanium.UI.createLabel({ 
     text:i, 
     height:50, 
     width:50, 
     borderRadius:25, 
     backgroundColor:'#336699'}); 
    scrollAlbums.add(circle[i]); 
} 
win1.add(scrollAlbums); 
+0

你已经有一些代码? – 2015-03-03 14:08:18

+0

你可以看看分页滚动。 http://docs.appcelerator.com/titanium/3.0/#!/api/Titanium.UI.ScrollableView – 2015-03-03 14:09:51

+0

您现有工作的代码将有所帮助。 – Martin 2015-03-03 19:16:51

回答

1

我修改你的代码滚动结束时记录中间的数字:

var win1 = Titanium.UI.createWindow({ 
    title:'Tab 1', 
    backgroundColor:'#fff'}); 
var scrollAlbums = Ti.UI.createScrollView({ 
    bottom: 10, 
    contentHeight: Ti.UI.SIZE, 
    contentWidth: Ti.UI.SIZE, 
    height: 95, 
    layout: 'horizontal', 
    showHorizontalScrollIndicator: false, 
    showVerticalScrollIndicator: true, 
    scrollType: 'horizontal', 
    horizontalWrap: false, 
    width: Ti.UI.FILL 
}); 
var circleWidth = 50; 
function pick(e) { 
    if (e.type === 'dragend' && e.decelerate) { 
    return; 
    } 
    console.info('Number: ' + Math.round((e.source.contentOffset.x + (e.source.size.width/2))/circleWidth)); 
} 
scrollAlbums.addEventListener('scrollend', pick); 
scrollAlbums.addEventListener('dragend', pick); 
var data=[]; 
var circle=[]; 
for(var i=0;i<20;i++){ 
    circle[i] = Titanium.UI.createLabel({ 
     text:i, 
     height:circleWidth, 
     width:circleWidth, 
     borderRadius:circleWidth/2, 
     backgroundColor:'#336699'}); 
    scrollAlbums.add(circle[i]); 
} 
win1.add(scrollAlbums); 
win1.open(); 

你需要听双方scrollenddragend因为scrollend会如果dragend正在减速,则仅会触发。然后用scrollView的偏移量加上它的总宽度的一半和圆的宽度,可以计算出中间数字。

但是就像Robin说的那样,你最好使用ScrollableView并且使用hitRectclipViews来播放不仅仅显示选定的页面(数字),而且还显示它的一些右侧和左侧。

+0

听众不工作.. scrollAlbums.addEventListener('scrollend',pick); scrollAlbums.addEventListener('dragend',pick); – GaneshKumar 2015-03-04 15:51:38

+0

你是怎么意思它不起作用的?我在这里测试了确切的粘贴代码,它对我来说工作得很好。 – 2015-03-05 07:50:14

+0

我试着完全没有编辑任何东西的代码,但scrollAlbums不听任何东西,也没有在控制台中打印任何东西。所以只有困惑.. – GaneshKumar 2015-03-05 14:59:20