【问题标题】:Get item clicked in horizontal list view created with scroll view获取在使用滚动视图创建的水平列表视图中单击的项目
【发布时间】:2023-04-08 13:17:01
【问题描述】:

我通过滚动视图创建了一个水平列表视图,但我不知道如何向其中添加项目点击事件并跟踪哪个项目被点击

<ScrollView id="scrollView"  showHorizontalScrollIndicator="true" height="20%" width="80%">
    <View id="containerView" backgroundColor="#336699" height="300" width="1000" layout='horizontal' />
</ScrollView>


var columns = [];
for(var index=0;index<15;index++){
    columns[index] = Ti.UI.createView({
        width   : 200,
        height  : 200,
        backgroundColor : '#123456',
        left    : 20 
    });
    $.containerView.add(columns[index]);
}


function doClick(item){
    console.log(item);
};

【问题讨论】:

    标签: listview titanium titanium-alloy horizontalscrollview


    【解决方案1】:

    您可以将侦听器添加到父视图,而不是向每个视图添加点击侦听器。并通过自定义 id 访问另一个子视图。 例如,在您的情况下使用以下代码。

        <ScrollView id="scrollView"  showHorizontalScrollIndicator="true" height="20%" width="80%">
                    <View id="containerView" onClick="onContainerViewClicked" backgroundColor="#336699" height="300" width="1000" layout='horizontal' />
        </ScrollView>
    
        var columns = [];
        for(var index=0;index<15;index++){
           columns[index] = Ti.UI.createView({
               width   : 200,
               height  : 200,
               backgroundColor : '#123456',
               left    : 20,
               id :index 
           });
           $.containerView.add(columns[index]);
        }
        $.containerView.width = 220 * 15;
    
        function onContainerViewClicked(e){
           alert(e.source.id);
           alert(columns[e.source.id]);
        }
    

    【讨论】:

      【解决方案2】:

      您必须为每个columns[index] 视图提供clickListener。这应该在你的 for 循环中执行。

      columns[index] = Ti.UI.createView({
          width   : 200,
          height  : 200,
          backgroundColor : '#123456',
          left    : 20 
      });
      
      columns[index].addEventListener('click', function(e) {
          // code here is run when the event is fired
          // properties of the event object 'e' describe the event and object that received it
          Ti.API.info('The '+e.type+' event happened');
      });
      
      $.containerView.add(columns[index]);
      

      不需要额外的函数doClick(Item),因为你可以内联定义它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-16
        • 1970-01-01
        • 2014-05-12
        • 2012-07-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多