Longpress event triggers tap event









up vote
0
down vote

favorite












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









share|improve this question























  • Elaborate your question.
    – Sunil Singh
    Nov 11 at 12:02










  • I have edited with a more specific question.
    – GeorgeK
    Nov 11 at 12:10














up vote
0
down vote

favorite












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









share|improve this question























  • Elaborate your question.
    – Sunil Singh
    Nov 11 at 12:02










  • I have edited with a more specific question.
    – GeorgeK
    Nov 11 at 12:10












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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









share|improve this question















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






angular events nativescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 13:44

























asked Nov 11 at 11:53









GeorgeK

548




548











  • Elaborate your question.
    – Sunil Singh
    Nov 11 at 12:02










  • I have edited with a more specific question.
    – GeorgeK
    Nov 11 at 12:10
















  • Elaborate your question.
    – Sunil Singh
    Nov 11 at 12:02










  • I have edited with a more specific question.
    – GeorgeK
    Nov 11 at 12:10















Elaborate your question.
– Sunil Singh
Nov 11 at 12:02




Elaborate your question.
– Sunil Singh
Nov 11 at 12:02












I have edited with a more specific question.
– GeorgeK
Nov 11 at 12:10




I have edited with a more specific question.
– GeorgeK
Nov 11 at 12:10












2 Answers
2






active

oldest

votes

















up vote
1
down vote













It would be much easier for you to define 2 events in your template code:



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

(tap)="functionWhenTap(item)"
(longPress)="functionWhenLongPress(item)"

orientation="horizontal"
class="preview-info-container"
>
</StackLayout>
</ng-template>
</ListView>


and then, handle it in your .ts file with these:



 functionWhenTap(item: any) 
// your things to do when tapped


functionWhenLongPress(item: any)
// your things to do when long pressed



This is an actual piece of code. Should work for you as well.



Here's working example, tested on my physical personal device:
https://play.nativescript.org/?template=play-ng&id=XgBfFE






share|improve this answer






















  • Thanks for your comment! I have tried this way, but i cannot prevent the tap event from being triggered at the end of the long press, thus when i console log i get two"longpressed" events, and then "tapped"
    – GeorgeK
    Nov 11 at 12:59










  • Well, that's interesting. Have you tried clearing your env. A little bit? Like, quit your emulator, remove node_modules, hooks and platforms, then npm i and tns platform add ios/android? Maybe something is cached. Maybe some tns build android/ios --clear? Start with these and we'll see.
    – ogrodowiczp
    Nov 11 at 13:03











  • And why the heck there are 2x long presses and 1x tap? Hmm.
    – ogrodowiczp
    Nov 11 at 13:05










  • Removed node_modules, hooks, and platform, and still getting exactly the same issue. Longpress is fired twice in the beginning and then tap is fired once.
    – GeorgeK
    Nov 11 at 13:21






  • 1




    Have you tried defining tap event one level higher - on ListView instead of it's child? I believe there's something like (itemTapped) if I recall well
    – ogrodowiczp
    Nov 11 at 13:52

















up vote
0
down vote













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.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248470%2flongpress-event-triggers-tap-event%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    It would be much easier for you to define 2 events in your template code:



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

    (tap)="functionWhenTap(item)"
    (longPress)="functionWhenLongPress(item)"

    orientation="horizontal"
    class="preview-info-container"
    >
    </StackLayout>
    </ng-template>
    </ListView>


    and then, handle it in your .ts file with these:



     functionWhenTap(item: any) 
    // your things to do when tapped


    functionWhenLongPress(item: any)
    // your things to do when long pressed



    This is an actual piece of code. Should work for you as well.



    Here's working example, tested on my physical personal device:
    https://play.nativescript.org/?template=play-ng&id=XgBfFE






    share|improve this answer






















    • Thanks for your comment! I have tried this way, but i cannot prevent the tap event from being triggered at the end of the long press, thus when i console log i get two"longpressed" events, and then "tapped"
      – GeorgeK
      Nov 11 at 12:59










    • Well, that's interesting. Have you tried clearing your env. A little bit? Like, quit your emulator, remove node_modules, hooks and platforms, then npm i and tns platform add ios/android? Maybe something is cached. Maybe some tns build android/ios --clear? Start with these and we'll see.
      – ogrodowiczp
      Nov 11 at 13:03











    • And why the heck there are 2x long presses and 1x tap? Hmm.
      – ogrodowiczp
      Nov 11 at 13:05










    • Removed node_modules, hooks, and platform, and still getting exactly the same issue. Longpress is fired twice in the beginning and then tap is fired once.
      – GeorgeK
      Nov 11 at 13:21






    • 1




      Have you tried defining tap event one level higher - on ListView instead of it's child? I believe there's something like (itemTapped) if I recall well
      – ogrodowiczp
      Nov 11 at 13:52














    up vote
    1
    down vote













    It would be much easier for you to define 2 events in your template code:



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

    (tap)="functionWhenTap(item)"
    (longPress)="functionWhenLongPress(item)"

    orientation="horizontal"
    class="preview-info-container"
    >
    </StackLayout>
    </ng-template>
    </ListView>


    and then, handle it in your .ts file with these:



     functionWhenTap(item: any) 
    // your things to do when tapped


    functionWhenLongPress(item: any)
    // your things to do when long pressed



    This is an actual piece of code. Should work for you as well.



    Here's working example, tested on my physical personal device:
    https://play.nativescript.org/?template=play-ng&id=XgBfFE






    share|improve this answer






















    • Thanks for your comment! I have tried this way, but i cannot prevent the tap event from being triggered at the end of the long press, thus when i console log i get two"longpressed" events, and then "tapped"
      – GeorgeK
      Nov 11 at 12:59










    • Well, that's interesting. Have you tried clearing your env. A little bit? Like, quit your emulator, remove node_modules, hooks and platforms, then npm i and tns platform add ios/android? Maybe something is cached. Maybe some tns build android/ios --clear? Start with these and we'll see.
      – ogrodowiczp
      Nov 11 at 13:03











    • And why the heck there are 2x long presses and 1x tap? Hmm.
      – ogrodowiczp
      Nov 11 at 13:05










    • Removed node_modules, hooks, and platform, and still getting exactly the same issue. Longpress is fired twice in the beginning and then tap is fired once.
      – GeorgeK
      Nov 11 at 13:21






    • 1




      Have you tried defining tap event one level higher - on ListView instead of it's child? I believe there's something like (itemTapped) if I recall well
      – ogrodowiczp
      Nov 11 at 13:52












    up vote
    1
    down vote










    up vote
    1
    down vote









    It would be much easier for you to define 2 events in your template code:



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

    (tap)="functionWhenTap(item)"
    (longPress)="functionWhenLongPress(item)"

    orientation="horizontal"
    class="preview-info-container"
    >
    </StackLayout>
    </ng-template>
    </ListView>


    and then, handle it in your .ts file with these:



     functionWhenTap(item: any) 
    // your things to do when tapped


    functionWhenLongPress(item: any)
    // your things to do when long pressed



    This is an actual piece of code. Should work for you as well.



    Here's working example, tested on my physical personal device:
    https://play.nativescript.org/?template=play-ng&id=XgBfFE






    share|improve this answer














    It would be much easier for you to define 2 events in your template code:



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

    (tap)="functionWhenTap(item)"
    (longPress)="functionWhenLongPress(item)"

    orientation="horizontal"
    class="preview-info-container"
    >
    </StackLayout>
    </ng-template>
    </ListView>


    and then, handle it in your .ts file with these:



     functionWhenTap(item: any) 
    // your things to do when tapped


    functionWhenLongPress(item: any)
    // your things to do when long pressed



    This is an actual piece of code. Should work for you as well.



    Here's working example, tested on my physical personal device:
    https://play.nativescript.org/?template=play-ng&id=XgBfFE







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 11 at 12:27

























    answered Nov 11 at 12:21









    ogrodowiczp

    216




    216











    • Thanks for your comment! I have tried this way, but i cannot prevent the tap event from being triggered at the end of the long press, thus when i console log i get two"longpressed" events, and then "tapped"
      – GeorgeK
      Nov 11 at 12:59










    • Well, that's interesting. Have you tried clearing your env. A little bit? Like, quit your emulator, remove node_modules, hooks and platforms, then npm i and tns platform add ios/android? Maybe something is cached. Maybe some tns build android/ios --clear? Start with these and we'll see.
      – ogrodowiczp
      Nov 11 at 13:03











    • And why the heck there are 2x long presses and 1x tap? Hmm.
      – ogrodowiczp
      Nov 11 at 13:05










    • Removed node_modules, hooks, and platform, and still getting exactly the same issue. Longpress is fired twice in the beginning and then tap is fired once.
      – GeorgeK
      Nov 11 at 13:21






    • 1




      Have you tried defining tap event one level higher - on ListView instead of it's child? I believe there's something like (itemTapped) if I recall well
      – ogrodowiczp
      Nov 11 at 13:52
















    • Thanks for your comment! I have tried this way, but i cannot prevent the tap event from being triggered at the end of the long press, thus when i console log i get two"longpressed" events, and then "tapped"
      – GeorgeK
      Nov 11 at 12:59










    • Well, that's interesting. Have you tried clearing your env. A little bit? Like, quit your emulator, remove node_modules, hooks and platforms, then npm i and tns platform add ios/android? Maybe something is cached. Maybe some tns build android/ios --clear? Start with these and we'll see.
      – ogrodowiczp
      Nov 11 at 13:03











    • And why the heck there are 2x long presses and 1x tap? Hmm.
      – ogrodowiczp
      Nov 11 at 13:05










    • Removed node_modules, hooks, and platform, and still getting exactly the same issue. Longpress is fired twice in the beginning and then tap is fired once.
      – GeorgeK
      Nov 11 at 13:21






    • 1




      Have you tried defining tap event one level higher - on ListView instead of it's child? I believe there's something like (itemTapped) if I recall well
      – ogrodowiczp
      Nov 11 at 13:52















    Thanks for your comment! I have tried this way, but i cannot prevent the tap event from being triggered at the end of the long press, thus when i console log i get two"longpressed" events, and then "tapped"
    – GeorgeK
    Nov 11 at 12:59




    Thanks for your comment! I have tried this way, but i cannot prevent the tap event from being triggered at the end of the long press, thus when i console log i get two"longpressed" events, and then "tapped"
    – GeorgeK
    Nov 11 at 12:59












    Well, that's interesting. Have you tried clearing your env. A little bit? Like, quit your emulator, remove node_modules, hooks and platforms, then npm i and tns platform add ios/android? Maybe something is cached. Maybe some tns build android/ios --clear? Start with these and we'll see.
    – ogrodowiczp
    Nov 11 at 13:03





    Well, that's interesting. Have you tried clearing your env. A little bit? Like, quit your emulator, remove node_modules, hooks and platforms, then npm i and tns platform add ios/android? Maybe something is cached. Maybe some tns build android/ios --clear? Start with these and we'll see.
    – ogrodowiczp
    Nov 11 at 13:03













    And why the heck there are 2x long presses and 1x tap? Hmm.
    – ogrodowiczp
    Nov 11 at 13:05




    And why the heck there are 2x long presses and 1x tap? Hmm.
    – ogrodowiczp
    Nov 11 at 13:05












    Removed node_modules, hooks, and platform, and still getting exactly the same issue. Longpress is fired twice in the beginning and then tap is fired once.
    – GeorgeK
    Nov 11 at 13:21




    Removed node_modules, hooks, and platform, and still getting exactly the same issue. Longpress is fired twice in the beginning and then tap is fired once.
    – GeorgeK
    Nov 11 at 13:21




    1




    1




    Have you tried defining tap event one level higher - on ListView instead of it's child? I believe there's something like (itemTapped) if I recall well
    – ogrodowiczp
    Nov 11 at 13:52




    Have you tried defining tap event one level higher - on ListView instead of it's child? I believe there's something like (itemTapped) if I recall well
    – ogrodowiczp
    Nov 11 at 13:52












    up vote
    0
    down vote













    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.






    share|improve this answer
























      up vote
      0
      down vote













      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.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 14:34









        GeorgeK

        548




        548



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248470%2flongpress-event-triggers-tap-event%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto