Merge pull request #114 from jsuarezruiz/fix-listview-refresh

Changes in ListViewRenderer to support source collection changes
This commit is contained in:
Frank A. Krueger 2018-03-23 11:25:14 -07:00 committed by GitHub
commit a9f044f171
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 142 additions and 3 deletions

View File

@ -1,5 +1,6 @@
using Ooui.Forms.Extensions; using Ooui.Forms.Extensions;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
using Xamarin.Forms; using Xamarin.Forms;
@ -22,6 +23,12 @@ namespace Ooui.Forms.Renderers
protected override void OnElementChanged(ElementChangedEventArgs<ListView> e) protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
{ {
if (e.OldElement != null)
{
var templatedItems = TemplatedItemsView.TemplatedItems;
templatedItems.CollectionChanged -= OnCollectionChanged;
}
if (e.NewElement != null) if (e.NewElement != null)
{ {
if (Control == null) if (Control == null)
@ -31,7 +38,10 @@ namespace Ooui.Forms.Renderers
SetNativeControl(_listView); SetNativeControl(_listView);
} }
UpdateItems(); var templatedItems = TemplatedItemsView.TemplatedItems;
templatedItems.CollectionChanged += OnCollectionChanged;
UpdateItems ();
UpdateBackgroundColor(); UpdateBackgroundColor();
} }
@ -54,10 +64,22 @@ namespace Ooui.Forms.Renderers
if (disposing && !_disposed) if (disposing && !_disposed)
{ {
if (Element != null)
{
var templatedItems = TemplatedItemsView.TemplatedItems;
templatedItems.CollectionChanged -= OnCollectionChanged;
}
_disposed = true; _disposed = true;
} }
} }
private void OnCollectionChanged (object sender, NotifyCollectionChangedEventArgs e)
{
UpdateItems ();
}
private void UnsubscribeCellClicks() private void UnsubscribeCellClicks()
{ {
foreach (var c in _cells) foreach (var c in _cells)
@ -71,6 +93,11 @@ namespace Ooui.Forms.Renderers
UnsubscribeCellClicks(); UnsubscribeCellClicks();
_cells.Clear(); _cells.Clear();
foreach (var child in _listView.Children)
{
_listView.RemoveChild (child);
}
var items = TemplatedItemsView.TemplatedItems; var items = TemplatedItemsView.TemplatedItems;
if (!items.Any()) if (!items.Any())

View File

@ -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>

View File

@ -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 ();
}
}
}

View File

@ -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 ();
}
}
}
}

View File

@ -34,6 +34,7 @@ namespace Samples
new DotMatrixClockSample().Publish(); new DotMatrixClockSample().Publish();
new EditorSample().Publish(); new EditorSample().Publish();
new MonkeysSample().Publish(); new MonkeysSample().Publish();
new RefreshListViewSample ().Publish ();
new SearchBarSample().Publish(); new SearchBarSample().Publish();
new SliderSample().Publish(); new SliderSample().Publish();
new SwitchListViewSample().Publish(); new SwitchListViewSample().Publish();

View File

@ -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);
}
}
}

View File

@ -32,6 +32,9 @@
<Compile Update="DisplayAlertPage.xaml.cs"> <Compile Update="DisplayAlertPage.xaml.cs">
<DependentUpon>DisplayAlertPage.xaml</DependentUpon> <DependentUpon>DisplayAlertPage.xaml</DependentUpon>
</Compile> </Compile>
<Compile Update="ListView\RefreshListView.xaml.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -44,6 +47,9 @@
<EmbeddedResource Update="DotMatrixClock\DotMatrixClockPage.xaml"> <EmbeddedResource Update="DotMatrixClock\DotMatrixClockPage.xaml">
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
</EmbeddedResource> </EmbeddedResource>
<EmbeddedResource Update="ListView\RefreshListView.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Monkeys\MonkeysView.xaml"> <EmbeddedResource Update="Monkeys\MonkeysView.xaml">
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
</EmbeddedResource> </EmbeddedResource>
@ -59,6 +65,7 @@
<None Remove="BugSweeper\Images\RedBug.png" /> <None Remove="BugSweeper\Images\RedBug.png" />
<None Remove="BugSweeper\Images\Xamarin120.png" /> <None Remove="BugSweeper\Images\Xamarin120.png" />
<None Remove="DotMatrixClock\DotMatrixClockPage.xaml" /> <None Remove="DotMatrixClock\DotMatrixClockPage.xaml" />
<None Remove="ListView\RefreshListView.xaml" />
<None Remove="Monkeys\Images\Baboon.jpg" /> <None Remove="Monkeys\Images\Baboon.jpg" />
<None Remove="Monkeys\Images\BlueMonkey.jpg" /> <None Remove="Monkeys\Images\BlueMonkey.jpg" />
<None Remove="Monkeys\Images\Capuchin.jpg" /> <None Remove="Monkeys\Images\Capuchin.jpg" />