Search code examples
angulareventsnativescript

Longpress event triggers tap event


In a list view I have two events happening, a tap event, and a longpress event, but longpress fires both. Inside the .html file:

<ListView class="listViewContainer" [items]="contactList">
  <ng-template let-item="item" let-i="index">
   <StackLayout 
    (loaded)="loaded($event)"
    orientation="horizontal"
    class="preview-info-container"
   >
   </StackLayout>
  </ng-template>
</ListView>

And then the .ts file

loaded(args) {
const element = args.object;
element.on("loaded, tap, longPress", (args) => {
  // console.log("Event: " + args.eventName + ", sender: " + args.object);
    if(args.eventName === "tap") {
      this.router.navigate(["card/contact/" + this.contact.id]);
    } else {
      this.togglePreviewOptions = !this.togglePreviewOptions;
    }
  });
}

My question is, how can I prevent the tap event from being fired when long pressing on the specific field?

This might be duplicate issue of NativeScript tap & longPress together not working, however since there has not been a clear answer I would like to raise it again.

Edit Some more info: The project tns version is

$ tns --version 4.3.0-2018-08-31-12160

Global nativescript version

nativescript@4.3.0-2018-08-31-12160

Emulator version:

Iphone 6, iOS 11.3

Solution

  • I have not been able to solve this issue by adding two different events (tap/longPress). As a solution i use the following

    Inside .html

       <StackLayout (touch)="onTouch($event)">
          <Contact-Preview [contact]=contactList[i]></Contact-Preview>
       </StackLayout>
    

    Inside .ts

    onTouch(args: TouchGestureEventData) {
      if(args.action === "down") {
        this.start = new Date().getMilliseconds();
      }
      if(args.action === "up") {
        this.end = new Date().getMilliseconds();
        const duration = Math.abs(this.start - this.end)
        console.log(duration > 150? "long press": "tap")
      }
    }
    

    This prevents tap and longPress events being fired on the same time, thus working around my issue.