Allow EmptyView swapping in Android CollectionView (#5720) fixes #5535

* Automated test for issue

* Fix empty view swapping for Android (fixes #5535)

* Remove private modifier

* Account for null Flags when setting flags for test

* Fix rebase issue
This commit is contained in:
E.Z. Hart 2019-04-03 10:49:05 -06:00 коммит произвёл Rui Marinho
Родитель b181e019c9
Коммит d1dc874dab
4 изменённых файлов: 87 добавлений и 5 удалений

Просмотреть файл

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
#if UITEST
using Xamarin.Forms.Core.UITests;
using Xamarin.UITest;
using NUnit.Framework;
#endif
namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[Category(UITestCategories.CollectionView)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 5535, "CollectionView: Swapping EmptyViews has no effect",
PlatformAffected.iOS | PlatformAffected.Android)]
public class Issue5535 : TestNavigationPage
{
protected override void Init()
{
#if APP
Device.SetFlags(new List<string>(Device.Flags ?? new List<string>()) { "CollectionView_Experimental" });
PushAsync(new GalleryPages.CollectionViewGalleries.EmptyViewGalleries.EmptyViewSwapGallery());
#endif
}
#if UITEST
[Test]
public void SwappingEmptyViews()
{
RunningApp.WaitForElement("FilterItems");
RunningApp.Tap("FilterItems");
RunningApp.EnterText("abcdef");
RunningApp.PressEnter();
// Default empty view
RunningApp.WaitForElement("Nothing to see here.");
RunningApp.Tap("ToggleEmptyView");
// Other empty view
RunningApp.WaitForElement("No results matched your filter.");
}
#endif
}
}

Просмотреть файл

@ -449,6 +449,7 @@
<DependentUpon>Issue5003.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue5535.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LegacyComponents\NonAppCompatSwitch.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MapsModalCrash.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ModalActivityIndicatorTest.cs" />

Просмотреть файл

@ -34,11 +34,11 @@
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<SearchBar x:Name="SearchBar" Placeholder="Filter" />
<SearchBar x:Name="SearchBar" Placeholder="Filter" AutomationId="FilterItems" />
<StackLayout Orientation="Horizontal" Grid.Row="1">
<Label Text="Toggle Between EmptyViews"/>
<Switch x:Name="EmptyViewSwitch"/>
<Switch x:Name="EmptyViewSwitch" AutomationId="ToggleEmptyView"/>
</StackLayout>
<CollectionView x:Name="CollectionView" Grid.Row="2">

Просмотреть файл

@ -10,10 +10,35 @@ namespace Xamarin.Forms.Platform.Android
{
public class EmptyViewAdapter : RecyclerView.Adapter
{
public object EmptyView { get; set; }
public DataTemplate EmptyViewTemplate { get; set; }
protected readonly ItemsView ItemsView;
int _itemViewType;
object _emptyView;
DataTemplate _emptyViewTemplate;
public object EmptyView
{
get => _emptyView;
set
{
_emptyView = value;
// Change _itemViewType to force OnCreateViewHolder to run again and use this new EmptyView
_itemViewType += 1;
}
}
public DataTemplate EmptyViewTemplate
{
get => _emptyViewTemplate;
set
{
_emptyViewTemplate = value;
// Change _itemViewType to force OnCreateViewHolder to run again and use this new template
_itemViewType += 1;
}
}
protected readonly ItemsView ItemsView;
public override int ItemCount => 1;
public EmptyViewAdapter(ItemsView itemsView)
@ -75,6 +100,11 @@ namespace Xamarin.Forms.Platform.Android
return new TemplatedItemViewHolder(itemContentView, template);
}
public override int GetItemViewType(int position)
{
return _itemViewType;
}
static TextView CreateTextView(string text, Context context)
{
var textView = new TextView(context) { Text = text };