Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 3abf5fa

Browse files
Jon2Gjsuarezruizjfversluis
authored
Solves [Bug] #595 Setting TabView.SelectedIndex does not "visually" switch tabs (#738)
* Solves [Bug] #595 Setting TabView.SelectedIndex does not "visually" switch tabs * Solves [Bug] #595 Validate the old and new index before invoking UpdateSelectedIndex as suggested * Fixes #630 Fixes #595 Added simple test Maybe related to #870 Co-authored-by: Javier Suárez <[email protected]> Co-authored-by: Gerald Versluis <[email protected]>
1 parent fb8c094 commit 3abf5fa

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/TabView/TabView.shared.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ static void OnSelectedIndexChanged(BindableObject bindable, object oldValue, obj
226226
{
227227
return;
228228
}
229-
230-
tabView.UpdateSelectedIndex(selectedIndex);
229+
if ((int)oldValue != selectedIndex)
230+
tabView.UpdateSelectedIndex(selectedIndex);
231231
}
232232
}
233233

@@ -477,8 +477,8 @@ void OnContentContainerPropertyChanged(object sender, PropertyChangedEventArgs e
477477
else if (e.PropertyName == nameof(CarouselView.Position))
478478
{
479479
var selectedIndex = contentContainer.Position;
480-
481-
UpdateSelectedIndex(selectedIndex, true);
480+
if (SelectedIndex != selectedIndex)
481+
UpdateSelectedIndex(selectedIndex, true);
482482
}
483483
}
484484

@@ -539,7 +539,8 @@ void AddTabViewItem(TabViewItem tabViewItem, int index = -1)
539539
UpdateTabContentSize();
540540
UpdateTabStripSize();
541541

542-
UpdateSelectedIndex(0);
542+
if (SelectedIndex != 0)
543+
UpdateSelectedIndex(0);
543544
}
544545

545546
void UpdateTabStripSize()
@@ -591,7 +592,10 @@ void AddSelectionTapRecognizer(View view)
591592
}
592593

593594
if (CanUpdateSelectedIndex(capturedIndex))
594-
UpdateSelectedIndex(capturedIndex);
595+
{
596+
if (SelectedIndex != capturedIndex)
597+
UpdateSelectedIndex(capturedIndex);
598+
}
595599
};
596600

597601
view.GestureRecognizers.Add(tapRecognizer);
@@ -707,8 +711,8 @@ void UpdateTabItemsSource()
707711

708712
UpdateTabContentSize();
709713
UpdateTabStripSize();
710-
711-
UpdateSelectedIndex(0);
714+
if (SelectedIndex != 0)
715+
UpdateSelectedIndex(0);
712716
}
713717

714718
void OnTabItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) => UpdateTabItemsSource();
@@ -749,18 +753,13 @@ void UpdateSelectedIndex(int position, bool hasCurrentItem = false)
749753
{
750754
if (position < 0)
751755
return;
756+
var oldposition = SelectedIndex;
752757

753-
var oldPosition = SelectedIndex;
754758
var newPosition = position;
755759

756-
if (oldPosition == newPosition)
757-
return;
758-
759-
SelectedIndex = newPosition;
760-
761760
Device.BeginInvokeOnMainThread(async () =>
762761
{
763-
if (contentTabItems == null)
762+
if (contentTabItems == null || contentTabItems.Count != TabItems.Count)
764763
contentTabItems = new ObservableCollection<TabViewItem>(TabItems.Where(t => t.Content != null));
765764

766765
var contentIndex = position;
@@ -773,7 +772,7 @@ void UpdateSelectedIndex(int position, bool hasCurrentItem = false)
773772
if (hasCurrentItem)
774773
currentItem = (TabViewItem)contentContainer.CurrentItem;
775774

776-
var tabViewItem = TabItems[SelectedIndex];
775+
var tabViewItem = TabItems[position];
777776

778777
contentIndex = contentTabItems.IndexOf(currentItem ?? tabViewItem);
779778
tabStripIndex = TabItems.IndexOf(currentItem ?? tabViewItem);
@@ -800,18 +799,19 @@ void UpdateSelectedIndex(int position, bool hasCurrentItem = false)
800799

801800
if (tabStripContent.Children.Count > 0)
802801
await tabStripContainerScroll.ScrollToAsync(tabStripContent.Children[tabStripIndex], ScrollToPosition.MakeVisible, false);
803-
});
804802

805-
if (newPosition != oldPosition)
806-
{
807-
var selectionChangedArgs = new TabSelectionChangedEventArgs()
803+
SelectedIndex = position;
804+
if (oldposition != SelectedIndex)
808805
{
809-
NewPosition = newPosition,
810-
OldPosition = oldPosition
811-
};
806+
var selectionChangedArgs = new TabSelectionChangedEventArgs()
807+
{
808+
NewPosition = newPosition,
809+
OldPosition = SelectedIndex
810+
};
812811

813-
OnTabSelectionChanged(selectionChangedArgs);
814-
}
812+
OnTabSelectionChanged(selectionChangedArgs);
813+
}
814+
});
815815
}
816816

817817
void OnCurrentTabItemSizeChanged(object sender, EventArgs e)

0 commit comments

Comments
 (0)