Changes in ListViewRenderer to support source collection changes (add, remove, etc)
This commit is contained in:
parent
0bd26e7519
commit
1fd1da699d
|
@ -1,5 +1,6 @@
|
|||
using Ooui.Forms.Extensions;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using Xamarin.Forms;
|
||||
|
@ -22,6 +23,12 @@ namespace Ooui.Forms.Renderers
|
|||
|
||||
protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
|
||||
{
|
||||
if (e.OldElement != null)
|
||||
{
|
||||
var templatedItems = TemplatedItemsView.TemplatedItems;
|
||||
templatedItems.CollectionChanged -= OnCollectionChanged;
|
||||
}
|
||||
|
||||
if (e.NewElement != null)
|
||||
{
|
||||
if (Control == null)
|
||||
|
@ -31,7 +38,10 @@ namespace Ooui.Forms.Renderers
|
|||
SetNativeControl(_listView);
|
||||
}
|
||||
|
||||
UpdateItems();
|
||||
var templatedItems = TemplatedItemsView.TemplatedItems;
|
||||
templatedItems.CollectionChanged += OnCollectionChanged;
|
||||
|
||||
UpdateItems ();
|
||||
UpdateBackgroundColor();
|
||||
}
|
||||
|
||||
|
@ -52,13 +62,25 @@ namespace Ooui.Forms.Renderers
|
|||
|
||||
base.Dispose(disposing);
|
||||
|
||||
if (disposing && !_disposed)
|
||||
if (disposing && !_disposed)
|
||||
{
|
||||
|
||||
if (Element != null)
|
||||
{
|
||||
var templatedItems = TemplatedItemsView.TemplatedItems;
|
||||
templatedItems.CollectionChanged -= OnCollectionChanged;
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void UnsubscribeCellClicks()
|
||||
private void OnCollectionChanged (object sender, NotifyCollectionChangedEventArgs e)
|
||||
{
|
||||
UpdateItems ();
|
||||
}
|
||||
|
||||
private void UnsubscribeCellClicks()
|
||||
{
|
||||
foreach (var c in _cells)
|
||||
{
|
||||
|
@ -71,6 +93,11 @@ namespace Ooui.Forms.Renderers
|
|||
UnsubscribeCellClicks();
|
||||
_cells.Clear();
|
||||
|
||||
foreach (var child in _listView.Children)
|
||||
{
|
||||
_listView.RemoveChild (child);
|
||||
}
|
||||
|
||||
var items = TemplatedItemsView.TemplatedItems;
|
||||
|
||||
if (!items.Any())
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<ContentPage
|
||||
xmlns="http://xamarin.com/schemas/2014/forms"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
|
||||
x:Class="Samples.RefreshListView">
|
||||
<ContentPage.Content>
|
||||
<StackLayout>
|
||||
<ListView
|
||||
ItemsSource="{Binding Data}"
|
||||
HeightRequest="200">
|
||||
<ListView.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<ViewCell>
|
||||
<Label
|
||||
Text="{Binding .}" />
|
||||
</ViewCell>
|
||||
</DataTemplate>
|
||||
</ListView.ItemTemplate>
|
||||
</ListView>
|
||||
<StackLayout>
|
||||
<Entry
|
||||
Text="{Binding Input}" />
|
||||
<Button
|
||||
Text="Add Item"
|
||||
Command="{Binding AddCmd}" />
|
||||
</StackLayout>
|
||||
</StackLayout>
|
||||
</ContentPage.Content>
|
||||
</ContentPage>
|
|
@ -0,0 +1,16 @@
|
|||
using Xamarin.Forms;
|
||||
using Xamarin.Forms.Xaml;
|
||||
|
||||
namespace Samples
|
||||
{
|
||||
[XamlCompilation(XamlCompilationOptions.Compile)]
|
||||
public partial class RefreshListView : ContentPage
|
||||
{
|
||||
public RefreshListView ()
|
||||
{
|
||||
InitializeComponent ();
|
||||
|
||||
BindingContext = new RefreshListViewModel ();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
using System.Collections.ObjectModel;
|
||||
using System.Windows.Input;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace Samples
|
||||
{
|
||||
public class RefreshListViewModel : BindableObject
|
||||
{
|
||||
public ICommand AddCmd => new Command (() => {
|
||||
Data.Add (Input);
|
||||
OnPropertyChanged (nameof (Data));
|
||||
});
|
||||
|
||||
private string _input;
|
||||
|
||||
public string Input
|
||||
{
|
||||
get => _input;
|
||||
set
|
||||
{
|
||||
_input = value;
|
||||
OnPropertyChanged ();
|
||||
}
|
||||
}
|
||||
|
||||
private ObservableCollection<string> _data = new ObservableCollection<string>();
|
||||
|
||||
public ObservableCollection<string> Data
|
||||
{
|
||||
get => _data;
|
||||
set
|
||||
{
|
||||
_data = value;
|
||||
OnPropertyChanged ();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -34,6 +34,7 @@ namespace Samples
|
|||
new DotMatrixClockSample().Publish();
|
||||
new EditorSample().Publish();
|
||||
new MonkeysSample().Publish();
|
||||
new RefreshListViewSample ().Publish ();
|
||||
new SearchBarSample().Publish();
|
||||
new SliderSample().Publish();
|
||||
new SwitchListViewSample().Publish();
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
using Ooui;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace Samples
|
||||
{
|
||||
public class RefreshListViewSample : ISample
|
||||
{
|
||||
public string Title => "Xamarin.Forms RefreshListView";
|
||||
|
||||
public Ooui.Element CreateElement ()
|
||||
{
|
||||
var page = new RefreshListView();
|
||||
return page.GetOouiElement ();
|
||||
}
|
||||
|
||||
public void Publish ()
|
||||
{
|
||||
UI.Publish ("/refreshlistview", CreateElement);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -32,6 +32,9 @@
|
|||
<Compile Update="DisplayAlertPage.xaml.cs">
|
||||
<DependentUpon>DisplayAlertPage.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Update="ListView\RefreshListView.xaml.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -44,6 +47,9 @@
|
|||
<EmbeddedResource Update="DotMatrixClock\DotMatrixClockPage.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</EmbeddedResource>
|
||||
<EmbeddedResource Update="ListView\RefreshListView.xaml">
|
||||
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
|
||||
</EmbeddedResource>
|
||||
<EmbeddedResource Update="Monkeys\MonkeysView.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</EmbeddedResource>
|
||||
|
@ -59,6 +65,7 @@
|
|||
<None Remove="BugSweeper\Images\RedBug.png" />
|
||||
<None Remove="BugSweeper\Images\Xamarin120.png" />
|
||||
<None Remove="DotMatrixClock\DotMatrixClockPage.xaml" />
|
||||
<None Remove="ListView\RefreshListView.xaml" />
|
||||
<None Remove="Monkeys\Images\Baboon.jpg" />
|
||||
<None Remove="Monkeys\Images\BlueMonkey.jpg" />
|
||||
<None Remove="Monkeys\Images\Capuchin.jpg" />
|
||||
|
|
Loading…
Reference in New Issue