项目作者: SyncfusionExamples

项目描述 :
Sample showcases how to load busy indicator to update each ListViewItem in xamarin.forms listview
高级语言: C#
项目地址: git://github.com/SyncfusionExamples/Load-busyindicator-to-update-each-listviewitem-xamarin.forms-listview.git


How to show busy indicator on ListViewItem

The SfListView allows displaying an activity indicator for an item when its data is being loaded in the background. To perform this, load both ActivityIndicator and a Button in the same row of a Grid element inside the ItemTemplate of the SfListView. The busy indicator and button can be enabled and disabled by using properties IsButtonVisible and IsIndicatorVisible respectively in the model class.

  1. public class BookInfo : INotifyPropertyChanged
  2. {
  3. private string bookName;
  4. private string bookDescription;
  5. public bool isDescriptionVisible;
  6. public bool isButtonVisible;
  7. public bool isIndicatorVisible;
  8. public BookInfo()
  9. {
  10. }
  11. public string BookName
  12. {
  13. get { return bookName; }
  14. set
  15. {
  16. bookName = value;
  17. OnPropertyChanged("BookName");
  18. }
  19. }
  20. public bool IsDescriptionVisible
  21. {
  22. get { return isDescriptionVisible; }
  23. set
  24. {
  25. isDescriptionVisible = value;
  26. OnPropertyChanged("IsDescriptionVisible");
  27. }
  28. }
  29. public string BookDescription
  30. {
  31. get { return bookDescription; }
  32. set
  33. {
  34. bookDescription = value;
  35. OnPropertyChanged("BookDescription");
  36. }
  37. }
  38. public bool IsButtonVisible
  39. {
  40. get { return isButtonVisible; }
  41. set
  42. {
  43. isButtonVisible = value;
  44. OnPropertyChanged("IsButtonVisible");
  45. }
  46. }
  47. public bool IsIndicatorVisible
  48. {
  49. get { return isIndicatorVisible; }
  50. set
  51. {
  52. isIndicatorVisible = value;
  53. OnPropertyChanged("IsIndicatorVisible");
  54. }
  55. }
  56. public event PropertyChangedEventHandler PropertyChanged;
  57. public void OnPropertyChanged(string name)
  58. {
  59. if (this.PropertyChanged != null)
  60. this.PropertyChanged(this, new PropertyChangedEventArgs(name));
  61. }
  62. }

Disable the visibility of Description and ActivityIndicator initially while adding items into collection.

  1. public class BookInfoRepository : INotifyPropertyChanged
  2. {
  3. private ObservableCollection<BookInfo> newBookInfo;
  4. public event PropertyChangedEventHandler PropertyChanged;
  5. public ObservableCollection<BookInfo> NewBookInfo
  6. {
  7. get { return newBookInfo; }
  8. set { this.newBookInfo = value; }
  9. }
  10. public void OnPropertyChanged(string name)
  11. {
  12. if (this.PropertyChanged != null)
  13. this.PropertyChanged(this, new PropertyChangedEventArgs(name));
  14. }
  15. public BookInfoRepository()
  16. {
  17. GenerateNewBookInfo();
  18. }
  19. private void GenerateNewBookInfo()
  20. {
  21. NewBookInfo = new ObservableCollection<BookInfo>();
  22. NewBookInfo.Add(new BookInfo() { BookName = "Machine Learning Using C#", BookDescription = "You’ll learn several different approaches to applying machine learning", IsIndicatorVisible = false, IsButtonVisible = true, IsDescriptionVisible = false });
  23. NewBookInfo.Add(new BookInfo() { BookName = "Object-Oriented Programming in C#", BookDescription = "Object-oriented programming is the de facto programming paradigm", IsIndicatorVisible = false, IsButtonVisible = true, IsDescriptionVisible = false });
  24. NewBookInfo.Add(new BookInfo() { BookName = "C# Code Contracts", BookDescription = "Code Contracts provide a way to convey code assumptions", IsIndicatorVisible = false, IsButtonVisible = true, IsDescriptionVisible = false });
  25. }
  26. }

Bind the bool values for the IsVisible properties to switch between indicator and button while loading the description.

  1. <ContentPage xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms">
  2. <ContentPage.BindingContext>
  3. <local:BookInfoRepository x:Name="ViewModel" ></local:BookInfoRepository>
  4. </ContentPage.BindingContext>
  5. <sync:SfListView x:Name="listView" AutoFitMode="Height" BackgroundColor="#d3d3d3" SelectionMode="None" ItemsSource="{Binding NewBookInfo}">
  6. <sync:SfListView.ItemTemplate>
  7. <DataTemplate>
  8. <Frame HasShadow="True" Margin="5,5,5,0">
  9. <Grid Padding="5">
  10. <Grid.RowDefinitions>
  11. <RowDefinition Height="*" ></RowDefinition>
  12. <RowDefinition Height="2*" ></RowDefinition>
  13. </Grid.RowDefinitions>
  14. <Label Text="{Binding BookName}" FontAttributes="Bold" FontSize="19" ></Label>
  15. <Button Grid.Row="1" Command="{Binding Path=BindingContext.IndicatorCommand, Source={x:Reference listView}}"
  16. CommandParameter="{Binding .}"
  17. Text="Load Description" IsVisible="{Binding IsButtonVisible}" HorizontalOptions="Center" VerticalOptions="Center"></Button>
  18. <Label Grid.Row="1" Text="{Binding BookDescription}" FontSize="15" IsVisible="{Binding IsDescriptionVisible}" ></Label>
  19. <ActivityIndicator Grid.Row="1" IsEnabled="True" IsRunning="True" IsVisible="{Binding IsIndicatorVisible}" ></ActivityIndicator>
  20. </Grid>
  21. </Frame>
  22. </DataTemplate>
  23. </sync:SfListView.ItemTemplate>
  24. </sync:SfListView>
  25. </ContentPage>

In the command of the Button, get the row data from its BindingContext and alter the bool values accordingly.

  1. public class BookInfoRepository:INotifyPropertyChanged
  2. {
  3. public Command<object> IndicatorCommand { get; set; }
  4. public BookInfoRepository()
  5. {
  6. IndicatorCommand = new Command<object>(OnLoadingItems);
  7. }
  8. private async void OnLoadingItems(object obj)
  9. {
  10. var model = obj as BookInfo;
  11. model.IsIndicatorVisible = true;
  12. model.IsButtonVisible = false;
  13. await Task.Delay(2000);
  14. model.IsDescriptionVisible = true;
  15. model.IsIndicatorVisible = false;
  16. }
  17. }

To know more about busy indicator on loading listview items, please refer our documentation here.