Chris Kooken
Chris Kooken

Reputation: 33938

Xamarin.Forms TapGestureRecognizer on listview not working when there are elements

I have a ListView I am using for chat messages. In order to dismiss the keyboard, I am listening for a tap gesture ANYWHERE inside the ListView. If the ListView has items, it never fires, but if there are no items, it fires just fine. What am I doing wrong?

I have also tried XamarinCommunityToolkit TouchEvents, but that won't work either. I can only get that to fire on the ContentView as a whole.

 public LiveEventChat() {
            InitializeComponent();

            var listviewgesture = new TapGestureRecognizer();
            listviewgesture.Tapped += Listviewgesture_Tapped;
            messageList.GestureRecognizers.Add(listviewgesture);        
        }

Upvotes: 1

Views: 335

Answers (1)

Wen xu Li
Wen xu Li

Reputation: 1686

You can add ItemTapped event to ListView, it will be triggered when ListView is clicked.

Here is the xaml code:

<StackLayout>
    <ListView x:Name="mytest" ItemTapped="mytest_ItemTapped">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Label Text="{Binding .}"></Label>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>        
</StackLayout>

Here is cs code:

 private void mytest_ItemTapped(object sender, ItemTappedEventArgs e)
 {
     //do something
 }

Using the above code, whenever you click on the ListView, the event will be triggered.

Upvotes: 1

Related Questions