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

Solves [Bug] #595 Setting TabView.SelectedIndex does not "visually" switch tabs #738

Merged
merged 6 commits into from
Feb 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ static void OnSelectedIndexChanged(BindableObject bindable, object oldValue, obj
{
return;
}

tabView.UpdateSelectedIndex(selectedIndex);
if ((int)oldValue != selectedIndex)
tabView.UpdateSelectedIndex(selectedIndex);
}
}

Expand Down Expand Up @@ -477,8 +477,8 @@ void OnContentContainerPropertyChanged(object sender, PropertyChangedEventArgs e
else if (e.PropertyName == nameof(CarouselView.Position))
{
var selectedIndex = contentContainer.Position;

UpdateSelectedIndex(selectedIndex, true);
if (SelectedIndex != selectedIndex)
UpdateSelectedIndex(selectedIndex, true);
}
}

Expand Down Expand Up @@ -539,7 +539,8 @@ void AddTabViewItem(TabViewItem tabViewItem, int index = -1)
UpdateTabContentSize();
UpdateTabStripSize();

UpdateSelectedIndex(0);
if (SelectedIndex != 0)
UpdateSelectedIndex(0);
}

void UpdateTabStripSize()
Expand Down Expand Up @@ -591,7 +592,10 @@ void AddSelectionTapRecognizer(View view)
}

if (CanUpdateSelectedIndex(capturedIndex))
UpdateSelectedIndex(capturedIndex);
{
if (SelectedIndex != capturedIndex)
UpdateSelectedIndex(capturedIndex);
}
};

view.GestureRecognizers.Add(tapRecognizer);
Expand Down Expand Up @@ -707,8 +711,8 @@ void UpdateTabItemsSource()

UpdateTabContentSize();
UpdateTabStripSize();

UpdateSelectedIndex(0);
if (SelectedIndex != 0)
UpdateSelectedIndex(0);
}

void OnTabItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) => UpdateTabItemsSource();
Expand Down Expand Up @@ -749,18 +753,13 @@ void UpdateSelectedIndex(int position, bool hasCurrentItem = false)
{
if (position < 0)
return;
var oldposition = SelectedIndex;

var oldPosition = SelectedIndex;
var newPosition = position;

if (oldPosition == newPosition)
return;

SelectedIndex = newPosition;

Device.BeginInvokeOnMainThread(async () =>
{
if (contentTabItems == null)
if (contentTabItems == null || contentTabItems.Count != TabItems.Count)
contentTabItems = new ObservableCollection<TabViewItem>(TabItems.Where(t => t.Content != null));

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

var tabViewItem = TabItems[SelectedIndex];
var tabViewItem = TabItems[position];

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

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

if (newPosition != oldPosition)
{
var selectionChangedArgs = new TabSelectionChangedEventArgs()
SelectedIndex = position;
if (oldposition != SelectedIndex)
{
NewPosition = newPosition,
OldPosition = oldPosition
};
var selectionChangedArgs = new TabSelectionChangedEventArgs()
{
NewPosition = newPosition,
OldPosition = SelectedIndex
};

OnTabSelectionChanged(selectionChangedArgs);
}
OnTabSelectionChanged(selectionChangedArgs);
}
});
}

void OnCurrentTabItemSizeChanged(object sender, EventArgs e)
Expand Down