Titanium 钛合金/合金/Appclerator:从列表项中删除事件侦听器

Titanium 钛合金/合金/Appclerator:从列表项中删除事件侦听器,titanium,appcelerator,appcelerator-titanium,titanium-alloy,appcelerator-alloy,Titanium,Appcelerator,Appcelerator Titanium,Titanium Alloy,Appcelerator Alloy,我有一个列表视图有点像这样: <ListView> <Templates> <ItemTemplate name="example"> <View id="wrapper" onClick="onClickExampleButton"> <Label>Click Me</Label> </View>

我有一个
列表视图
有点像这样:

<ListView>
    <Templates>
        <ItemTemplate name="example">
            <View id="wrapper" onClick="onClickExampleButton">
                <Label>Click Me</Label>
            </View>
        </ItemTemplate>
    </Templates>

    <ListSection id="ls">
        <ListItem template="example"></ListItem>
        <ListItem template="example"></ListItem>
        <ListItem template="example"></ListItem>
    </ListSection>
</ListView>
function onClickExampleButton(e) {
     var item = $.ls.getItemAt(e.itemIndex);
     // TODO: I want to disable the onClick eventListener here

     someLongAsyncFuncToServer(function() {
         // TODO: I want to re-enable the onClick eventListener here
     })
}
<ListView>
    <Templates>
        <ItemTemplate name="example">
            <View id="wrapper" onClick="onClickExampleButton">
                <Label touchEnabled="false">Click Me</Label>
            </View>
        </ItemTemplate>
    </Templates>

    <ListSection id="ls">
        <ListItem template="example"></ListItem>
        <ListItem template="example"></ListItem>
        <ListItem template="example"></ListItem>
    </ListSection>
</ListView>
通常删除事件侦听器非常简单

$.objId.removeEventListener(onClickExampleButton)
重新添加它非常简单,如下所示:

$.objId.addEventListener(onClickExampleButton)

但是,我不知道如何在
列表项上实现这一点,我将在对象上放置一个属性,并使用它来确定状态。例如

单击按钮时设置变量,然后在长时间运行异步函数后更改它。。。这样,如果状态为running,则忽略单击。一旦它不再运行,然后接受单击。

我会在对象上放置一个属性,并使用它来确定状态。例如
单击按钮时设置变量,然后在长时间运行异步函数后更改它。。。这样,如果状态为running,则忽略单击。一旦它不再运行,就接受单击。

我相信您可以通过使用元素触发的事件的源id来实现这一点。您需要注意的唯一一件事是,事件被冒泡到父层次结构,因此任何子视图都可以调用click事件,从而提供意外的源ID

要解决您的查询,您可以安全地使用以下代码:

function onClickExampleButton(e) {
     var item = $.ls.getItemAt(e.itemIndex);

     // TODO: I want to disable the onClick eventListener here
     e.source.touchEnabled = false;

     someLongAsyncFuncToServer(function() {
         // TODO: I want to re-enable the onClick eventListener here
         e.source.touchEnabled = true;
     })
}
并对XML代码进行如下更改:

<ListView>
    <Templates>
        <ItemTemplate name="example">
            <View id="wrapper" onClick="onClickExampleButton">
                <Label>Click Me</Label>
            </View>
        </ItemTemplate>
    </Templates>

    <ListSection id="ls">
        <ListItem template="example"></ListItem>
        <ListItem template="example"></ListItem>
        <ListItem template="example"></ListItem>
    </ListSection>
</ListView>
function onClickExampleButton(e) {
     var item = $.ls.getItemAt(e.itemIndex);
     // TODO: I want to disable the onClick eventListener here

     someLongAsyncFuncToServer(function() {
         // TODO: I want to re-enable the onClick eventListener here
     })
}
<ListView>
    <Templates>
        <ItemTemplate name="example">
            <View id="wrapper" onClick="onClickExampleButton">
                <Label touchEnabled="false">Click Me</Label>
            </View>
        </ItemTemplate>
    </Templates>

    <ListSection id="ls">
        <ListItem template="example"></ListItem>
        <ListItem template="example"></ListItem>
        <ListItem template="example"></ListItem>
    </ListSection>
</ListView>

点击我
这里的问题是,首先在视图(id=wrapper)中的标签上设置touchEnabled='false',它将确保单击事件不会由标签触发,而只由父级触发

接下来,在click事件方法中,您使用的是e.source,它现在是您的包装器视图


如果未在标签上设置touchEnabled=false,则e.source也可以包含标签引用。您可以阅读有关事件冒泡的更多信息,这将帮助您了解如何在Tianium中高效地处理事件。

我相信您可以通过使用元素触发的事件的源id来实现这一点。您需要注意的唯一一件事是,事件被冒泡到父层次结构,因此任何子视图都可以调用click事件,从而提供意外的源ID

要解决您的查询,您可以安全地使用以下代码:

function onClickExampleButton(e) {
     var item = $.ls.getItemAt(e.itemIndex);

     // TODO: I want to disable the onClick eventListener here
     e.source.touchEnabled = false;

     someLongAsyncFuncToServer(function() {
         // TODO: I want to re-enable the onClick eventListener here
         e.source.touchEnabled = true;
     })
}
并对XML代码进行如下更改:

<ListView>
    <Templates>
        <ItemTemplate name="example">
            <View id="wrapper" onClick="onClickExampleButton">
                <Label>Click Me</Label>
            </View>
        </ItemTemplate>
    </Templates>

    <ListSection id="ls">
        <ListItem template="example"></ListItem>
        <ListItem template="example"></ListItem>
        <ListItem template="example"></ListItem>
    </ListSection>
</ListView>
function onClickExampleButton(e) {
     var item = $.ls.getItemAt(e.itemIndex);
     // TODO: I want to disable the onClick eventListener here

     someLongAsyncFuncToServer(function() {
         // TODO: I want to re-enable the onClick eventListener here
     })
}
<ListView>
    <Templates>
        <ItemTemplate name="example">
            <View id="wrapper" onClick="onClickExampleButton">
                <Label touchEnabled="false">Click Me</Label>
            </View>
        </ItemTemplate>
    </Templates>

    <ListSection id="ls">
        <ListItem template="example"></ListItem>
        <ListItem template="example"></ListItem>
        <ListItem template="example"></ListItem>
    </ListSection>
</ListView>

点击我
这里的问题是,首先在视图(id=wrapper)中的标签上设置touchEnabled='false',它将确保单击事件不会由标签触发,而只由父级触发

接下来,在click事件方法中,您使用的是e.source,它现在是您的包装器视图

如果未在标签上设置touchEnabled=false,则e.source也可以包含标签引用。您可以阅读更多关于事件冒泡的信息,这将帮助您了解如何在Tianium中高效地处理事件