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

Implemented colored refresh indicator for ListView pull-to-refresh #2961

Merged
merged 16 commits into from Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -5,7 +5,7 @@
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;

using System.Threading.Tasks;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

Expand Down Expand Up @@ -268,6 +268,16 @@ protected override void Build(StackLayout stackLayout)
fastScrollItemContainer.View.On<Android>().SetIsFastScrollEnabled(true);
fastScrollItemContainer.View.ItemsSource = viewModel.CategorizedEmployees;

var coloredSpinnerContainer = new ViewContainer<ListView>(Test.ListView.ColoredSpinner, new ListView());
InitializeElement(coloredSpinnerContainer.View);
coloredSpinnerContainer.View.SpinnerColor = Color.Yellow;
coloredSpinnerContainer.View.IsPullToRefreshEnabled = true;
coloredSpinnerContainer.View.Refreshing += async (object sender, EventArgs e) => {
await Task.Delay(2000);
coloredSpinnerContainer.View.IsRefreshing = false;
};
coloredSpinnerContainer.View.ItemsSource = viewModel.Employees;

Add(groupDisplayBindingContainer);
Add(groupHeaderTemplateContainer);
Add(groupShortNameContainer);
Expand All @@ -280,6 +290,7 @@ protected override void Build(StackLayout stackLayout)
Add(rowHeightContainer);
Add(selectedItemContainer);
Add(fastScrollItemContainer);
Add(coloredSpinnerContainer);
}
}
}
8 changes: 8 additions & 0 deletions Xamarin.Forms.Core/ListView.cs
Expand Up @@ -46,6 +46,8 @@ public class ListView : ItemsView<Cell>, IListViewController, IElementConfigurat

public static readonly BindableProperty SeparatorColorProperty = BindableProperty.Create("SeparatorColor", typeof(Color), typeof(ListView), Color.Default);

public static readonly BindableProperty SpinnerColorProperty = BindableProperty.Create(nameof(SpinnerColor), typeof(Color), typeof(ListView), Color.Black);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should use the Color.Defaultand in the renderers you need to capture the default color of the spinner when they are initialised , so if the user changes back to the default color you can set the default color of the platform.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did Color.Default, but then the spinner disappeared for some weird reason, at least on iOS. I'll check it again.


readonly Lazy<PlatformConfigurationRegistry<ListView>> _platformConfigurationRegistry;

BindingBase _groupDisplayBinding;
Expand Down Expand Up @@ -217,6 +219,12 @@ public Color SeparatorColor
set { SetValue(SeparatorColorProperty, value); }
}

public Color SpinnerColor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RefreshControlColor? because it could be something different from a spinner in a specific platform.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm makes sense. I was struggling with the name. RefreshControlColor seems to cover it.

{
get { return (Color)GetValue(SpinnerColorProperty); }
set { SetValue(SpinnerColorProperty, value); }
}

public SeparatorVisibility SeparatorVisibility
{
get { return (SeparatorVisibility)GetValue(SeparatorVisibilityProperty); }
Expand Down
3 changes: 2 additions & 1 deletion Xamarin.Forms.CustomAttributes/TestAttributes.cs
Expand Up @@ -428,7 +428,8 @@ public enum ListView
GroupDisplayBinding,
GroupShortNameBinding,
ScrollTo,
FastScroll
FastScroll,
ColoredSpinner
}

public enum TableView
Expand Down
9 changes: 9 additions & 0 deletions Xamarin.Forms.Platform.Android/Renderers/ListViewRenderer.cs
Expand Up @@ -163,6 +163,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
UpdateIsSwipeToRefreshEnabled();
UpdateFastScrollEnabled();
UpdateSelectionMode();
UpdateSpinnerColor();
}
}

Expand Down Expand Up @@ -200,6 +201,8 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
UpdateFastScrollEnabled();
else if (e.PropertyName == ListView.SelectionModeProperty.PropertyName)
UpdateSelectionMode();
else if (e.PropertyName == ListView.SpinnerColorProperty.PropertyName)
UpdateSpinnerColor();
}

protected override void OnLayout(bool changed, int l, int t, int r, int b)
Expand Down Expand Up @@ -415,6 +418,12 @@ void UpdateSelectionMode()
}
}

void UpdateSpinnerColor()
{
if (_refresh != null)
_refresh.SetColorSchemeColors(Element.SpinnerColor.ToAndroid());
}

internal class Container : ViewGroup
{
IVisualElementRenderer _child;
Expand Down
16 changes: 16 additions & 0 deletions Xamarin.Forms.Platform.iOS/Renderers/ListViewRenderer.cs
Expand Up @@ -248,6 +248,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
UpdateSeparatorColor();
UpdateSeparatorVisibility();
UpdateSelectionMode();
UpdateSpinnerColor();

var selected = e.NewElement.SelectedItem;
if (selected != null)
Expand Down Expand Up @@ -286,6 +287,8 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
UpdatePullToRefreshEnabled();
else if (e.PropertyName == Xamarin.Forms.ListView.SelectionModeProperty.PropertyName)
UpdateSelectionMode();
else if (e.PropertyName == Xamarin.Forms.ListView.SpinnerColorProperty.PropertyName)
UpdateSpinnerColor();
}

NSIndexPath[] GetPaths(int section, int index, int count)
Expand Down Expand Up @@ -673,6 +676,13 @@ void UpdateSelectionMode()
}
}

void UpdateSpinnerColor()
{
var color = Element.SpinnerColor;

if (_tableViewController != null)
_tableViewController.UpdateSpinnerColor(color.ToUIColor());
}

internal class UnevenListViewDataSource : ListViewDataSource
{
Expand Down Expand Up @@ -1409,6 +1419,12 @@ public override void ViewWillAppear(bool animated)
UpdateIsRefreshing(true);
}

public void UpdateSpinnerColor(UIColor color)
{
if (RefreshControl != null)
RefreshControl.TintColor = color;
}

protected override void Dispose(bool disposing)
{
if (_disposed)
Expand Down