Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Grouped Listview Expandable

$
0
0

Hello,
I create a GroupedListview with a DataTemplateSelector to have diferent margins on the viewcells of the listview, now what i need is to hide the content of every group and when the user cliks on the group name the group expandes, how can i do this?

Here are my project files:

Listview page:

https://paste.ofcode.org/pBXr3xbFZZPXMwFbZ37Ggm

ViewModel:

`public class ProductsViewModel: BindableBase
{

    public ObservableCollection<Product> Productitems { get; set; }




    public ObservableCollection<Grouping<string, Product>> Item { get; set; }





    public ProductsViewModel()
    {
        Productitems = new ObservableCollection<Product>
        {
            new Product
            {
                Img = "teste.png",
                Url = "Teste",
                Category = "Service",
                Title = "sdsadsadsdsdsa"


            },
            new Product
            {
                Img = "teste.png",
                Url = "Teste3",
                Category = "Service",
                Title = "sdsadsadsdsdsatest2"
            },
            new Product
            {
                Img = "teste.png",
                Url = "Teste2",
                Category = "School",
                Title = "sdsadsadsdsdsa"

            },
            new Product
            {
                Img = "teste.png",
                Url = "Teste4",
                Category = "Farmacy",
                Title = "sdsadsadsdsdsa"

            },
            new Product
            {
                Img = "teste.png",
                Url = "Teste7",
                Category = "Farmacy",
                Title = "sdsadsadsdsdsa"
            },
            new Product
            {
                Img = "teste.png",
                Url = "Teste7",
                Category = "Farmacy",
                Title = "sdsadsadsdsdsa"
            },
            new Product
            {
                Img = "teste.png",
                Url = "Teste9",
                Category = "School",
                Title = "sdsadsadsdsd"

            }
        };


         var sorted = from Products in Productitems
                     group Products by Products.Category into ProductsGroup
            select new Grouping<string, Models.Product>(Products Group.Key, ProductsGroup);

        Item = new ObservableCollection<Grouping<string, Models.Product>>(sorted);




    }


}`

DataTemplateSelector:

´public class AlternateColorDataTemplateSelector2 : DataTemplateSelector
{

    public DataTemplate EvenTemplate { get; set; }
    public DataTemplate UnevenTemplate { get; set; }

    private List<Product> flatList;

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        // TODO: Maybe some more error handling here
        if (flatList == null)
        {
            var groupedList = (ObservableCollection<Grouping<string, Product>>)((ListView)container).ItemsSource;
            flatList = groupedList.SelectMany(group => group).ToList();
        }

        return flatList.IndexOf(item as Product) % 2 == 0 ? EvenTemplate : UnevenTemplate;
    }
}´

Thanks


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>