Xamarin.forms: ItemSelected/ItemTapped is not called in listview if an item view has TapGestureRecognizer (Android)

Created on 25 Mar 2018  Â·  28Comments  Â·  Source: xamarin/Xamarin.Forms

Description

ItemSelected (and ItemTapped) is not called on a listview, if it has label with TapGestureRecognizer:

<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
......
<Label1>
<Label2>
......
<LabelX>
<LabelX.GestureRecognizers>
<TapGestureRecognizer Tapped="OnLabelXClick" CommandParameter="{Binding .}"/>
</LabelX.GestureRecognizers>

Tapped 'OnLabelXClick' event is called on the LabelX, but the ItemSelected/ItemTapped event is not called in the ListView if you click outside of LabelX position.

This used to work in Xamarin.Forms.2.5.0.122203 (but with other issue: https://github.com/xamarin/Xamarin.Forms/issues/1331), but after upgrading to Xamarin.Forms.3.0.0.296286-pre2, ItemSelected/ItemTapped is not called.

This is only on Android. On iOS, works fine.
Might be related to this fix: https://github.com/xamarin/Xamarin.Forms/issues/1331

Steps to Reproduce

  1. Create ListView in XAML
  2. Create ItemTemplate
  3. Make sure ItemTemplate ViewCell has several views, and at least a Label, which has TapGestureRecognizer
  4. In C# / ContentPage constructor add event handler to ListView.ItemSelected : _listView.ItemSelected += async (sender, e) => { ........}

Expected Behavior

If you click on a list item:
-click on the label with TapGestureRecognizer: Tapped function is called [this works]
-click on other place of the item in the list: ItemSelected/ItemTapped event triggered [this does NOT work]

Actual Behavior

If you click on a list item:
-click on the label with TapGestureRecognizer: Tapped function is called
-click on other place of the item in the list: ItemSelected/ItemTapped event is NOT triggered

Basic Information

  • Version with issue: Xamarin.Forms.3.0.0.296286-pre2
  • Last known good version: Xamarin.Forms.2.5.0.122203
  • IDE: VS2017 15.6.4
  • Platform Target Frameworks:

    • iOS: NOT REPRODUCIBLE, works fine. (11.1)

    • Android: 8.1

  • Android Support Library Version:
  • Nuget Packages:
  • Affected Devices:
listview 5 help wanted high impact Android bug up-for-grabs

Most helpful comment

For me, the ItemSelected/ItemTapped is not called in listview if an item view has TapGestureRecognizer and ContextActions. Xamarin.Forms 3.3.0.912540

I'm adding a repro project

FastScroll.zip

All 28 comments

ListViewBug.zip

Sample attached to reproduce.

If you run, and you click on any list item (outside of text1 label), you can see that the "OnItemSelected" is not called,

If you remove the from the ListViewBug\MainPage.xaml , "OnItemSelected" is called properly.

I am having the same issue when using Xamarin.Forms 2.5.1.392594-pre3.
Removing the GestureRecognizer from a child Grid inside the ItemTemplate ViewCell worked to get the OnItemSelected called.

This is only occurring on Android. It works fine on iOS targeting iOS 11.2.
IDE: VS2017 15.6.4

3.0.0.427558-pre4
Still there, reproducible

3.0.0.446417
Still there, reproducible

Will this be fixed in a new version for Xamarin.Forms 2.5.1? That would be great! Because XF 3.0 requires VS 2017 ...

[offtopic]
I find it funny, that a fix for a bug introduces a new bug. Including a ListView_ItemTapped method would immediately show that error in the Github1331 project.

The issue is reported two months ago and since then the fix isn't planned nor the issue solved. The origin issue is here since six months! Xamarin.Forms seems still not be really useful for productive environments ...
[/offtopic]

See also #3210.

Still an issue on XF 3.1.0.637273

I have the same issue on iOS with XF 3.1.0, iOS 11.4

Normally, if user taps on view which has TapGestureRecognizer, that should be called.
Otherwise, if user taps on view which has NO TapGestureRecognizer / or taps outside of any view within the item, then ItemSelected/ItemTapped should be called.

Other idea could be that ItemTapped event receives the tapped view as param (which is NULL if tapped on "empty" area). This case, no need for TapGestureRecognizer.

So:
class ItemTappedEventArgs { object Group, object Item)
-->
class ItemTappedEventArgs { object Group, object Item, object View)

I've got a few TapGestureRecognizers within a viewcell, they can co-exist.

On iOS, everything works - tapping viewcell will invoke "ItemSelected" and tapping on the GestureRecognizers will invoke the various commands.

On Android, only the GestureRecognizers work, the viewcell "ItemSelected" no longer functions.

Both iOS and Android were working on XF 2.5. I upgraded to 3.1.0 last night and after testing that's the one thing that doesn't work on Android.

Same issue here. I did discover that if I commented out my ViewCell.ContextActions that the GestureRecognizer and ItemSelected event both now work on Android. Of course I've been testing all day with iOS only to find discover this bug on Android now...

I worked around this by adding long press and short press effects.

Confirming that this issue still exists in 3.1.0.697729. I didn't have any luck with effects. So I added an image to my viewcell with a Image.tapGesture and removed the ViewCell.ContextActions. This had the ItemSelected work again and the tap gesture on the image works. Maybe that will help someone.

Hi Matt, sorry - just caught this update. I can share my effects code if it helps. Let me know

Hey, thanks for the reply. I'd be happy to see the code. Since I never got it working (:


From: James English notifications@github.com
Sent: Thursday, August 23, 2018 5:04:27 PM
To: xamarin/Xamarin.Forms
Cc: Matt Butler; Comment
Subject: Re: [xamarin/Xamarin.Forms] ItemSelected/ItemTapped is not called in listview if an item view has TapGestureRecognizer (Android) (#2180)

Hi Matt, sorry - just caught this update. I can share my effects code if it helps. Let me know

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHubhttps://github.com/xamarin/Xamarin.Forms/issues/2180#issuecomment-415585896, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AMVsCzwkJ4aG6CVJt-0Wd0evNB-zB1OUks5uTybrgaJpZM4S58kj.

Journyx, Inc.
7600 Burnet Road #300
Austin, TX 78757
www.journyx.com

p 512.834.8888
f 512-834-8858

Do you receive our promotional emails? Click here http://journyx.com/communication-preferences or visit http://journyx.com/communication-preferences to unsubscribe.

OK no problem. I'll try to explain it all here.

Since the ListView is working with iOS, I didn't want to modify that to have to work with long / short press and keep the swipe mechanism so for that I created two DataTemplates, one for iOS and one for Android:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/creating

The following line will choose which template to use based on the platform....

<local:PlatformSelector x:Key="PlatformSelector" isPlatformiOS="{StaticResource iOSPlatformTemplate}" isPlatformAndroid="{StaticResource AndroidPlatformTemplate}" />

and on the ListView itself...

<ListView ItemTemplate="{StaticResource PlatformSelector}" ... >

code behind:

    public class PlatformSelector : DataTemplateSelector
    {
        public DataTemplate isPlatformiOS { get; set; }
        public DataTemplate isPlatformAndroid { get; set; }

        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            return Device.RuntimePlatform.Equals(Device.iOS) ? isPlatformiOS : isPlatformAndroid;
        }
    }

On the Android ListView viewcell I had a StackLayout and attached the effect:

                                <StackLayout.Effects>
                                     <effects:LongPressedEffect/>
                                </StackLayout.Effects>

The code for effect, I used this example:

https://alexdunn.org/2017/12/27/xamarin-tip-xamarin-forms-long-press-effect/

and code from here: https://github.com/lywyn/LongPressedTestApp

If you're stuck, give me a shout.

Context Action and Item Tapped does not work together on Android.

For me, the ItemSelected/ItemTapped is not called in listview if an item view has TapGestureRecognizer and ContextActions. Xamarin.Forms 3.3.0.912540

I'm adding a repro project

FastScroll.zip

For me, the ItemSelected/ItemTapped is not called in listview if an item view has TapGestureRecognizer and ContextActions. Xamarin.Forms 3.3.0.912540

I'm adding a repro project

FastScroll.zip

I have same issue https://forums.xamarin.com/discussion/145672/listview-itemselected-and-itemtapped-not-working-with-contextactions/

Same any fix ? Thanks

I posted a solution above...

Thanks for the solution @jveltd. This has been driving me absolutely mental for a good six hours now. I've been reading the documentation and the samples over and over assuming that I was doing something wrong.

@russticate @jveltd
Note that jveltd code is not a solution, but a workaround. Which is ok for a moment, but this is clearly a bug in Xamarin Forms / Android, which should be fixed.

@zoli13 is correct, my implementation is a workaround for the bug.

This exact bug appeared many versions ago and was fixed. It's subsequently re-appeared and due to this we went for a work-around so we didn't have to keep waiting for bugs to be fixed.

@jveltd I understand. Although I think I actually prefer the effect over a ContextAction personally. When applied to a ViewCell its got a consistent behaviour across both platforms. Regardless, thanks for the help!

It works correctly in Android but it is not working in iOS if view cell has context action.

I have a reproduction case for this issue: A ListView containing items that have a gesture recognizer as well as a context action. In this case, the ItemTapped event is never called:
https://gist.github.com/vividos/d2819d8e3890413593c04e9d622ceee8
Add this file to the Xamarin.Forms.Controls.Issues.Shared project and start the Xamarin.Forms.ControlGallery.Android project.
I hope this helps to find and fix the issue.

Was this page helpful?
0 / 5 - 0 ratings