Search code examples
c#xamarin.formscollectionview

RefreshView in CollectionView Xamarin.Forms


I am trying to use RefreshView in my CollectionView, but I have problem. when I change manually data in my viewmodels, then I try to refresh with pulled down my screen, refresh circle is appear but my data is not updated

Am I missing something?

This is my code

View

<ContentPage.Content>
    <RefreshView IsRefreshing="{Binding IsRefreshing}"
                 Command="{Binding RefreshCommand}">
        <CollectionView ItemsSource="{Binding ListOfColor}"
                    Margin="10"
                    SelectionMode="Single">
            <CollectionView.ItemsLayout>
                <GridItemsLayout Orientation="Vertical"/>
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Grid ColumnSpacing="7" RowSpacing="5">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="150"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <BoxView BackgroundColor="{Binding Color}"
                            Grid.Column="0" Grid.Row="0" Grid.RowSpan="2"
                            HeightRequest="60"
                            WidthRequest="60" />
                        <Label Text="{Binding ColorName}"
                        Grid.Column="1" Grid.Row="0"
                        TextColor="{Binding Color}"
                        FontAttributes="Bold"/>
                        <Label Text="{Binding ColorDesc}"
                        Grid.Column="1" Grid.Row="1"
                        TextColor="{Binding Color}"/>
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </RefreshView>
</ContentPage.Content>

ViewModel

class ColorCollectionViewModel : INotifyPropertyChanged
{
    const int RefreshDuration = 2;
    bool isRefreshing;

    public bool IsRefreshing
    {
        get
        {
            return isRefreshing;
        }
        set
        {
            isRefreshing = value;
            OnPropertyChanged();
        }
    }

    public ObservableCollection<ListColorModel> ListOfColor { get; private set; }

    public ICommand RefreshCommand => new Command(async () => await RefreshDataAsync());

    public ColorCollectionViewModel()
    {
        ListOfColor = new ObservableCollection<ListColorModel>();
        NewData();
    }

    void NewData()
    {
        ListOfColor.Add(new ListColorModel { ColorName = "Hitam", Color = "Black", ColorDesc = "Ini Warna Hitam" });
        ListOfColor.Add(new ListColorModel { ColorName = "Coklat", Color = "Brown", ColorDesc = "Ini Warna Coklat" });
        ListOfColor.Add(new ListColorModel { ColorName = "Merah", Color = "Red", ColorDesc = "Ini Warna Merah" });
        ListOfColor.Add(new ListColorModel { ColorName = "Orange", Color = "Orange", ColorDesc = "Ini Warna Orange" });
        ListOfColor.Add(new ListColorModel { ColorName = "Kuning", Color = "Yellow", ColorDesc = "Ini Warna Kuning" });
        ListOfColor.Add(new ListColorModel { ColorName = "Hijau", Color = "Green", ColorDesc = "Ini Warna Hijau" });
        ListOfColor.Add(new ListColorModel { ColorName = "Biru", Color = "Blue", ColorDesc = "Ini Warna Blue" });
        ListOfColor.Add(new ListColorModel { ColorName = "Ungu", Color = "Purple", ColorDesc = "Ini Warna Ungu" });
        ListOfColor.Add(new ListColorModel { ColorName = "Abu-Abu", Color = "Gray", ColorDesc = "Ini Warna Abu-Abu" });
        ListOfColor.Add(new ListColorModel { ColorName = "Pink", Color = "Pink", ColorDesc = "Ini Warna Pink" });

        OnPropertyChanged(nameof(ListOfColor));
    }

    async Task RefreshDataAsync()
    {
        IsRefreshing = true;
        await Task.Delay(TimeSpan.FromSeconds(RefreshDuration));
        IsRefreshing = false;
    }

    #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion
}

Solution

  • When you pull down the RefreshView command that you bound to RefreshDataAsync will execute but you are not changing any data inside of it try with something like:

    Only for illustration, it will clear all data and load only one

        async Task RefreshDataAsync()
        {
            IsRefreshing = true;
            ListOfColor.clear();
            ListOfColor.Add(new ListColorModel { ColorName = "Pink", Color = "Pink", ColorDesc = "Ini Warna Pink" });
            await Task.Delay(TimeSpan.FromSeconds(RefreshDuration));
            OnPropertyChanged(nameof(ListOfColor));
            IsRefreshing = false;
        }