ItemSelected event is not firing when a SwipeView is inside a ListView

Kaushalya Dharmarathna 21 Reputation points
2021-07-05T06:52:29.34+00:00

Hi All,

I have tested SwipeView's functionalities perfectly with a ListView. But when I wrapped it inside a ContentView, swipe actions are working but ListView's ItemSelected event is not firing.

Sample code:

<?xml version="1.0" encoding="utf-8" ?>  
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
             x:Class="App2.MainPage">  
    <StackLayout>  
        <ListView x:Name="EmployeeView"  
              ItemSelected="EmployeeView_ItemSelected">  
            <ListView.ItemTemplate>  
                <DataTemplate>  
                    <ViewCell>  
                        <ContentView>  
                            <SwipeView>  
                                <SwipeView.LeftItems>  
                                    <SwipeItems>  
                                        <SwipeItem Text="Favorite"  
                                               BackgroundColor="Blue"/>  
                                        <SwipeItem Text="Delete"  
                                               BackgroundColor="LightPink" />  
                                    </SwipeItems>  
                                </SwipeView.LeftItems>  
                                <StackLayout Orientation="Horizontal"  
                                         BackgroundColor="GreenYellow">  
                                    <Label Text="{Binding Name}"/>  
                                    <Label Text="{Binding Age}"/>  
                                </StackLayout>  
                            </SwipeView>  
                        </ContentView>  
                    </ViewCell>  
                </DataTemplate>  
            </ListView.ItemTemplate>  
        </ListView>  
    </StackLayout>  
</ContentPage>  

I'm using XF 5.0.0.2012 and only tested with Android.

In my real requirement I'm using a grouped ListView where most of the groups don't need swipe actions. When list items count getting larger observed some lags in Swipe items (eg. icons) loading. Therefore thought to use the SwipeView only if needed else using a read only Grid. So planned to set the ContentView based on a Trigger.

Is this a known issue or I'm doing something wrong?
If this approach is not correct can you please suggest me a proper way.

@JessieZhang-MSFT , have seen you in SwipeView related threads. Any input on this?

Thanks
Kaushalya

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
0 comments No comments
{count} votes

Accepted answer
  1. Kyle Wang 5,531 Reputation points
    2021-07-06T03:06:15.98+00:00

    Hi KaushalyaDharmarathna-0277,

    Welcome to our Microsoft Q&A platform!

    It is a known issue on Android, and you can follow up the issue here:

    https://github.com/xamarin/Xamarin.Forms/issues/9466

    Or submit a new issue on github.

    A workaround to handle this is using CollectionView.
    xaml

    <CollectionView x:Name="EmployeeView"  
                ItemsSource="{Binding Employees}"  
                SelectionMode="Single"  
                SelectionChanged="EmployeeView_SelectionChanged"  
                SelectedItem="{Binding SelectedEmployee, Mode=TwoWay}"  
                SelectionChangedCommand="{Binding SelectedCommand}">  
        <CollectionView.ItemTemplate>  
            <DataTemplate>  
                    <ContentView>  
                        <SwipeView>  
                            <SwipeView.LeftItems>  
                                <SwipeItems>  
                                    <SwipeItem Text="Favorite"  
                                            BackgroundColor="Blue"/>  
                                    <SwipeItem Text="Delete"  
                                            BackgroundColor="LightPink" />  
                                </SwipeItems>  
                            </SwipeView.LeftItems>  
                            <StackLayout Orientation="Horizontal"  
                                        BackgroundColor="GreenYellow">  
                                <Label Text="{Binding Name}"/>  
                                <Label Text="{Binding Age}"/>  
                            </StackLayout>  
                        </SwipeView>  
                    </ContentView>  
            </DataTemplate>  
        </CollectionView.ItemTemplate>  
    </CollectionView>  
    

    viewmodel:

    class MainPageViewModel : INotifyPropertyChanged  
    {  
        public ICommand SelectedCommand { get; set; }  
        public List<Employee> Employees { get; set; }  
        public MainPageViewModel()  
        {  
            Employees = new List<Employee>();  
            for (int i = 0; i < 10; i++)  
            {  
                Employees.Add(new Employee() { Name = "Name" + i, Age = "Age" + i });  
            }  
            SelectedCommand = new Command(Select);  
        }  
      
        void Select()  
        {  
            if (SelectedEmployee != null)  
            {  
                Console.WriteLine(SelectedEmployee.Name);  
                // do something  
            }  
            SelectedEmployee = null;  
        }  
      
        Employee selectedEmployee;  
        public Employee SelectedEmployee  
        {  
            get => selectedEmployee;  
            set  
            {  
                selectedEmployee = value;  
                OnPropertyChanged("SelectedEmployee");  
            }  
        }  
      
        public event PropertyChangedEventHandler PropertyChanged;  
      
        protected void OnPropertyChanged(string propertyName)  
        {  
            var handler = PropertyChanged;  
            if (handler != null)  
                handler(this, new PropertyChangedEventArgs(propertyName));  
        }  
    }  
    

    Regards,
    Kyle


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful