NativeScript: ListView itemTap does not fire when there is a button in the ListView.item template
When a ListView.itemTemplate
contains a button element, the itemTap
event does not trigger anymore.
test.xml
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
<ListView items="{{ items }}" itemTap="onTap">
<ListView.itemTemplate>
<StackLayout>
<Label text="{{ $value }}" />
<Button text="Button" />
</StackLayout>
</ListView.itemTemplate>
</ListView>
</Page>
test.js
var observable = require("data/observable");
var observableArray = require("data/observable-array");
var items = ['one', 'two', 'three', 'four'];
var viewModel = {
items: items
};
exports.pageLoaded = function (args) {
var page = args.object;
page.bindingContext = viewModel;
};
exports.onTap = function (args) {
var index = args.index;
console.log('Clicked item with index ' + index);
};
I’m not sure if this is made on purpose, but for me it was very hard to figure out why the itemTap
event doesn’t fire.
If this behaviour is intended, it should be mentioned in the ListView
documentation.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 3
- Comments: 21 (3 by maintainers)
Looks like this is known issue with buttons inside ListView in Android
Here is an example how to do this in {N}:
Ah, gotcha. Thanks for the enlightenment 😄
@Jayuda, as far as I remember I haven’t tried @enchev’s workaround, because in the end I just used a label with on-tap event instead of a button.