Group ListView by a component on a JSON in Xamarin.Forms

前端 未结 1 1435
孤街浪徒
孤街浪徒 2021-01-17 05:25

I\'m trying to group items from a JSON in the ListView.

My JSON is stored in an web repository, I already pulled it from the server and listed it but ungrouped.

1条回答
  •  北荒
    北荒 (楼主)
    2021-01-17 06:13

    Check out this article: https://xamarinhelp.com/xamarin-forms-listview-grouping/

    But better to use ObservableCollection, where the above article uses List.

    So you have to create an ObservableCollection of a type that is a subclass of ObservableCollection.

    First create a type that subclasses ObservableCollection that will hold one group of companies by status:

    public class CompanyByStatusList : ObservableCollection
    {
        public string Status { get; set; }
        public ObservableCollection Companies => this;
    }
    

    Then create an ObservableCollection of CompanyByStatusList. This will be your ItemsSource for your ListView.

    public ObservableCollection CompanyList { get; set; }
    

    Then you want to create a CompanyByStatusList for each status that holds all of the companies in that specific status, and then add each of those CompanyByStatusList to the CompanyList collection. Make sure to set the Status property of each CompanyByStatusList

    And make sure to set IsGroupingEnabled="true" on your ListView. ListView xaml:

    
        
            
                
                    
            
        
    
        
            
                
                    
                        

    0 讨论(0)
提交回复
热议问题