Titanium手机 - 一个视图上的addEventListener [英] Titanium mobile - addEventListener on a view

查看:141
本文介绍了Titanium手机 - 一个视图上的addEventListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用常用的JS进行滚动菜单。

i'm making a scrollable menu using common JS.

菜单项是包含2个其他组件的视图:
a imageView for图标和该菜单文本的标签。

An item of the menu is a view that contains 2 others components : a imageView for the icon, and a label for the text of this menu.

该软件在Android和ios模拟器上是奇怪的,不一样。

The comportement is strange and not the same on the android and ios simulator.

在android上,如果在标签或imageview上完成了一个点击,那将给出一个:未捕获TypeError:无法读取属性...
在iphone上, t启动任何东西。

On the android, if a click is done on the label or on the imageview, that gives an : "uncaught TypeError: Cannot Read property..." On iphone, that just don't launch anything.

如果我点击别的地方(仍然在视图项目中),但不是在图像上或在Labal上,例如在边缘,这只是完美的!!

If i click somewhere else (still into the view item) but not on image or on the labal, for example on an edge, that works just perfect!!

这里是代码:

function menuIcons(itemTab){

function menuIcons(itemTab) {

var menuMain = Ti.UI.createView({
    layout : 'vertical',
    backgroundColor : '#333333',
    height : 125,
    bottom : 10,
    left : 10,
    right : 10,
    borderRadius : 5.0
});

var menuFirstLine = Ti.UI.createScrollView({
    scrollType : 'horizontal',
    contentHeight : 120,
    contentWidth : 'auto',
    layout : 'horizontal',
    height : 120,
    marginLeft : 5
});

var items = [];
var menuIconsItem = require('view/module/menuIconsItem');

for(var i in itemTab) {
    var page = itemTab[i].page;

    items[i] = new menuIconsItem(itemTab[i]);

    (function(itemsEvent) {
        itemsEvent.id = itemTab[i].id;
        itemsEvent.addEventListener('click', function(e) {

            Ti.App.fireEvent('main_menu_' + itemsEvent.id, {
                id : e.source.id
            });
        })
    })(items[i]);

    menuFirstLine.add(items[i]);
}
menuMain.add(menuFirstLine);
return menuMain;

}

module.exports = menuIcons ;

module.exports = menuIcons;

和所需项目的代码(var menuIconsItem = require('view / module / menuIconsItem');):

and the code of the items that is required (var menuIconsItem = require('view/module/menuIconsItem');) :

功能菜单IconsItem(item){

function menuIconsItem(item) {

// path for images on Android  besoin de centraliser tout ca
var pathImages = '';

var itemImage = Ti.UI.createImageView({
    image : item.imageLink,
    width : 64,
    height : 64,
    top : 15
});

var itemLabel = Ti.UI.createLabel({
    color : '#afafaf',
    text : item.text,
    font : {
        textAlign : 'center'
    },
    height : 40,
    top : 80
});

var menuItem = Ti.UI.createView({
    width : 120,
    height : 120,
    backgroundColor : '#424242',
    top : 5,
    left : 5
});

menuItem.add(itemImage);
menuItem.add(itemLabel);

return menuItem;

}

module.exports = menuIconsItem ;

module.exports = menuIconsItem;

推荐答案

您还必须设置标签和图像视图的ID。

You have to set the id for the label and image view as well.

这篇关于Titanium手机 - 一个视图上的addEventListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆