How to handle the tapped event when child view is clicked in Android platform?
In Android platform, the clicked event will be triggered for both child view loaded in the ItemTemplate and ListView’s interaction events: ItemTapped, ItemDoubleTapped ItemHold, while interaction.
For example, when a button loaded inside the ListView is clicked, the ListView’s interaction event and button click will get triggered. So, you can handle the interaction event by overriding the OnClick method of ListViewItemRenderer in Android renderer to trigger the click event of child view alone while interaction.
To achieve this, extend the ListViewItem and return to the extended ItemGenerator class which is set as ItemGenerator.
XAML
<ContentPage xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"> <sync:SfListView x:Name="listView" TapCommand="{Binding TapCommand}" ItemsSource="{Binding BookInfo}"> <sync:SfListView.Behaviors> <local1:Behaviours/> </sync:SfListView.Behaviors> <sync:SfListView.ItemTemplate> <DataTemplate> <Grid Padding="10" BackgroundColor="Aqua"> <Grid.GestureRecognizers> <TapGestureRecognizer Command="{Binding Path = BindingContext.GestureCommand, Source={x:Reference listView}}"/> </Grid.GestureRecognizers> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="100" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="0.4*" /> <RowDefinition Height="0.6*" /> </Grid.RowDefinitions> <Label Text="{Binding BookName}" FontAttributes="Bold"/> <Button Text="Test" Command="{Binding BindingContext.ButtonCommand, Source={x:Reference listView}}" Grid.Row="1"/> </Grid> </DataTemplate> </sync:SfListView.ItemTemplate> </sync:SfListView> </ContentPage>
C#
public class Behaviours : Behavior<SfListView> { private SfListView listView; protected override void OnAttachedTo(BindableObject bindable) { listView = bindable as SfListView; listView.ItemGenerator = new ItemGeneratorExt(listView); base.OnAttachedTo(bindable); } } public class ItemGeneratorExt : ItemGenerator { public ItemGeneratorExt(SfListView listview) : base(listview) { } protected override ListViewItem OnCreateListViewItem(int rowIndex, ItemType type, object data =null) { if (type == ItemType.Record) return new ListViewItemExt(); return base.OnCreateListViewItem(rowIndex, type, data); } } public class ListViewItemExt : ListViewItem { }
C#
[assembly: ExportRenderer(typeof(ListViewItemExt), typeof(ItemRendererExt))] namespace GettingStarted { public class ItemRendererExt : ListViewItemRenderer { public override void OnClick(Android.Views.View view) { return; } } }
When both TapCommand and TapGestureRecognizer of Grid are defined simulataneously, the functions will perform in the following order: TapGestureRecognizer and then TapCommand.
When handling the item tapped event, the list view’s selection operations will not be performed.
Click here to download the sample.