首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android/ios: Titanium水平滚动

Android/ios: Titanium水平滚动
EN

Stack Overflow用户
提问于 2015-03-03 19:42:49
回答 1查看 1.3K关注 0票数 0

我正在为Android/I设计一个视图,我需要设计一个时间选择器(水平)。我已经设计了水平视图,并在其中输入值。

但是不知道如何选择箭头下的值。以及最后一个数字将如何居中(在箭头下)。请指点一下。

代码:

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

发布于 2015-03-04 19:12:09

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

代码语言:javascript
复制
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,因为只有在dragend减速的情况下,scrollend才会触发。然后,用scrollView的偏移量加上其总宽度的一半和圆的宽度,就可以计算出中间的数字。

但就像罗宾说的那样,你最好使用ScrollableView,使用hitRectclipViews,不仅显示选定的页面(编号),还显示其中的几个左右部分。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28830943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档