Forum Xamarin.Forms

Same listview item not getting selected after pressing back

SiddySiddy Member ✭✭

I have a listview which on any item selected navigates to another XAML page. When I press the back button I return back to the original listview page.

Now if I select the same list view item, it does not navigate to the intended XAML page. If I select another item in listview it navigates.

So the issue I am facing is that tapping/selecting the same item does not navigate to the intended page the second time after the back button is pressed, but any other item in the list does navigate. Also the break point is not hit in VS (onitemtapped event args), so can't debug the issue too.

Any help/suggestions on the same?.

Thanks In Advanvce!!..

Tagged:

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    Which event are you using ? I think you can try with ItemTapped, or you should unselect the item


    https://docs.microsoft.com/it-it/xamarin/xamarin-forms/user-interface/listview/interactivity#selectiontaps
  • SiddySiddy Member ✭✭

    I am using "ItemTapped" property of listview on XAML and implementing the event in code behind.

    Also in the end, after page navigated I make the selected item null again.

    Here is my code:

    private async void listview_ItemSelected(object sender, SelectedItemChangedEventArgs e)
            {
                if(e.SelectedItem == null)
                {
                    return;
                }
                Person selectedItem = e.SelectedItem as Person;
                //DisplayAlert("title", selectedItem.FirstName + " " + selectedItem.LastName, "OK");
                await Navigation.PushAsync(new SubEntries(selectedItem));
    
                selectedItem = null;
            }
    
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    Maybe a bug?
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    Ok @JoeManke but ItemTapped should raise the event every time the item is tapped or not?
  • JoeMankeJoeManke USMember ✭✭✭✭✭

    I would think so but I don't ever use ItemTapped, and the code @Siddy posted is an ItemSelected handler.

  • SiddySiddy Member ✭✭

    Using the following line help resolve it:

    ((ListView)sender).SelectedItem = null;
    

    The only catch here is that, the OnItemTap event, gets called twice, due to the above mentioned line, but now as the selected item is null, it returns or comes out of the event method.

    if(e.SelectedItem == null)
                {
                    return;
                }
    
  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    Using ItemSelected, the behaviour you are seeing is expected. The even is raised on change of item selected - so tapping the same item again won't fire the event.

    As you have found, clearing the SelectedItem solves this.

    However, this clears the indication of the selected item on the UI. So ItemTapped may be a better option.

    Also you should really be using MVVM and not code behind.
  • franArfranAr Member

    @AlessandroCaliaro said:
    Maybe a bug?

    I agree with Alessandro. It is a "Mode=TwoWay" binding issue. I set "selectedItem = null" and forced Initialize component OnAppearing() method:

    protected override void OnAppearing()
    {
    // Fix error Grid Mode=TwoWay
    InitializeComponent();
    }

    It solve the issue of Binding two way mode!

  • Menno.5004Menno.5004 USMember ✭✭

    @franAr said:

    @AlessandroCaliaro said:
    Maybe a bug?

    I agree with Alessandro. It is a "Mode=TwoWay" binding issue. I set "selectedItem = null" and forced Initialize component OnAppearing() method:

    protected override void OnAppearing()
    {
    // Fix error Grid Mode=TwoWay
    InitializeComponent();
    }

    It solve the issue of Binding two way mode!

    Sure this will work, but completely unnecessary. You should (probably?) never reinitialize your component everytime the page appears. It's better the reset the SelectedItem in OnAppearing.

    e.g. this.ListViewName.SelectedItem = null;

  • nsoft_sunilnsoft_sunil Member ✭✭

    Just Set SelectionMode = "None"
    call the ItemTapped Event..
    Thats it...
    I tried and worked

Sign In or Register to comment.