Attempts to get horizontal list to work

This commit is contained in:
watsonb8
2019-07-10 17:17:10 -04:00
parent 823e1341ca
commit c99f752174
6 changed files with 190 additions and 125 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows.Input;
using Xamarin.Forms;
@ -13,6 +14,7 @@ namespace Aurora.Design.Components.HorizontalList
private readonly StackLayout _itemsStackLayout;
public event EventHandler SelectedItemChanged;
private NotifyCollectionChangedEventHandler _itemsChangedHandler;
public StackOrientation ListOrientation { get; set; }
@ -22,7 +24,12 @@ namespace Aurora.Design.Components.HorizontalList
BindableProperty.Create("SelectedCommand", typeof(ICommand), typeof(HorizontalList), null);
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(HorizontalList), default(IEnumerable<object>), BindingMode.TwoWay, propertyChanged: ItemsSourceChanged);
BindableProperty.Create("ItemsSource",
typeof(ObservableCollection<object>),
typeof(HorizontalList),
default(ObservableCollection<object>),
BindingMode.TwoWay,
propertyChanged: ItemsSourceChanged);
public static readonly BindableProperty SelectedItemProperty =
BindableProperty.Create("SelectedItem", typeof(object), typeof(HorizontalList), null, BindingMode.TwoWay, propertyChanged: OnSelectedItemChanged);
@ -36,9 +43,9 @@ namespace Aurora.Design.Components.HorizontalList
set { SetValue(SelectedCommandProperty, value); }
}
public IEnumerable ItemsSource
public ObservableCollection<object> ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
get { return (ObservableCollection<object>)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
@ -56,12 +63,13 @@ namespace Aurora.Design.Components.HorizontalList
private static void ItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
{
var itemsLayout = (HorizontalList)bindable;
HorizontalList itemsLayout = bindable as HorizontalList;
itemsLayout.SetItems();
}
public HorizontalList()
{
_itemsChangedHandler = OnItemsChanged;
// BackgroundColor = Color.FromHex("#1E2634");
Spacing = 6;
_scrollView = new ScrollView();
@ -78,6 +86,11 @@ namespace Aurora.Design.Components.HorizontalList
Children.Add(_scrollView);
}
~HorizontalList()
{
ItemsSource.CollectionChanged -= _itemsChangedHandler;
}
protected virtual void SetItems()
{
_itemsStackLayout.Children.Clear();
@ -99,6 +112,9 @@ namespace Aurora.Design.Components.HorizontalList
return;
}
//Setup collection events
ItemsSource.CollectionChanged += _itemsChangedHandler;
foreach (var item in ItemsSource)
{
_itemsStackLayout.Children.Add(GetItemView(item));
@ -127,7 +143,6 @@ namespace Aurora.Design.Components.HorizontalList
};
AddGesture(view, gesture);
return view;
}
@ -164,5 +179,33 @@ namespace Aurora.Design.Components.HorizontalList
itemsView.SelectedCommand?.Execute(newValue);
}
}
private void OnItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
{
foreach (object item in e.NewItems)
{
_itemsStackLayout.Children.Add(GetItemView(item));
}
break;
}
case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
{
foreach (object item in e.OldItems)
{
//Clear layout and rebuild
_itemsStackLayout.Children.Clear();
foreach (var source in ItemsSource)
{
_itemsStackLayout.Children.Add(GetItemView(item));
}
}
break;
}
}
}
}
}

View File

@ -57,7 +57,7 @@ namespace Aurora.Design.Components.MemberList
if (membersList != null)
{
_newSource = newValue as ObservableCollection<PartyMember>;
membersList.ItemsSource = newValue as ObservableCollection<PartyMember>;
membersList.ItemsSource = newValue as ObservableCollection<object>;
}
}
}