Selecting an already Highlighted item in a ListView does not call the SelectedItem method.

toyinstoyins ✭✭✭USMember ✭✭✭
edited March 21 in Xamarin.Forms

When I click on an Item in a ListView, it calls the SelectedItem method and the code takes me to the details page.
However, when I click on the back button to come back to the Main Page and click on exactly the same item nothing happens.
If I click on another item on the list, it calls the method but not the current one.
Is this a bug in ListView or is it meant to work this way?
Here's the Xaml

    <ListView x:Name="CarListView" ItemsSource="{Binding Items}"
                      CachingStrategy="RecycleElement"
                      SelectedItem="{Binding SelectedCar}"
                      RowHeight="60">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Margin="8">
                                <Label Text="{Binding Make}"
                               FontAttributes="Bold" />
                                <Label Text="{Binding YearOfModel}" />
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

Best Answer

Answers

  • JamesLaveryJamesLavery ✭✭✭✭✭ GBBeta, University ✭✭✭✭✭
    > @AlessandroCaliaro said:
    > You can use ItemTapped

    Indeed. The behaviour you are seeing is the design of ListView. The item is still selected so tapping it again will not fire the SelectedItem method.
  • LandLuLandLu Xamurai Member, Xamarin Team Xamurai

    @toyins If you don't want this default behavior, you could uncheck the list view's selection when the user clicks the back button to come back:

    protected override void OnAppearing()
    {
        base.OnAppearing();
    
        var viewModel = BindingContext as MainPageViewModel;
        viewModel.SelectedCar = null;
    }
    
  • JamesLaveryJamesLavery ✭✭✭✭✭ GBBeta, University ✭✭✭✭✭

    @LandLu said:
    @toyins If you don't want this default behavior, you could uncheck the list view's selection when the user clicks the back button to come back:

    protected override void OnAppearing()
    {
        base.OnAppearing();
    
        var viewModel = BindingContext as MainPageViewModel;
        viewModel.SelectedCar = null;
    }
    

    Yes, this is an approach we've used. Ideally, if your MVVM framework exposes Appearing events in the ViewModel (which FreshMvvm and MvvmCross do), you can/should do this in the ViewModel rather than in xaml.cs code behind.

    Another consideration is whether the user experience is good if they tap to go to the item details, then when they go back the item is no longer selected. This could be considered a bad UX feature.

  • toyinstoyins ✭✭✭ USMember ✭✭✭
    edited March 21

    @LandLu, @JamesLavery The onAppearing does not work. The ItemTapped suggested by @AlessandroCaliaro seems to work but the SelectedCar in the ViewModel is called twice by the SelectedItem Method and the ItemTapped Event.

    I guess, I have to use the ItemTapped Event instead of the SelectedItem method.

  • LandLuLandLu Xamurai Member, Xamarin Team Xamurai
    @toyins You could add an if statement, do the push operation when the value != null.
  • toyinstoyins ✭✭✭ USMember ✭✭✭

    @LandLu, where do I add the if statement and how? Please expatiate.

  • LandLuLandLu Xamurai Member, Xamarin Team Xamurai

    @toyins If you want to navigate to the detail page in the SelectedCar's set method with MVVM, you could try:

    Model _selectedCar;
    public Model SelectedCar
    {
        set
        {
            _selectedCar = value;
            OnPropertyChanged();
            if (value != null)
            {
                // Do the push
            }
        }
        get
        {
            return _selectedCar;
        }
    }
    

    Or you could use ItemTapped event instead.

  • toyinstoyins ✭✭✭ USMember ✭✭✭

    @LandLu oh, that's how it's currently set up.

  • chetanrawatchetanrawat ✭✭✭ USMember ✭✭✭

    Hi @toyins,
    try this code

    private async void ListViewItemSelected(object sender, SelectedItemChangedEventArgs e)
    {

                if ((sender != null && ListView.SelectedItem != null))
                {
    
                }
                educationListView.SelectedItem = null;
        }
    
  • GianJuarexGianJuarex USMember
    edited June 19

    You are totally wrong omg. You made waste time. Totally.
    I just had to create a GestureRecognizer like this
    <Frame.GestureRecognizers>

    </Frame.GestureRecognizers>

    Later, in my viewmodel i received the command always i pushed my main frame.
    Consider this solution if you need and answer for this problem.
    Grettings.

    Edit: I dont know why the code doesn't show. But the solucion is you use a TapGestureRecognizer inside a GestureRecognizers

Sign In or Register to comment.