Is there a way to have 2 different objects following one another in a CollectionView in Xamarin.Forms?

634 Views Asked by At

I have a CollectionView that shows a list of Categories object like so: enter image description here

And I'd like to have a list of SubCategories to be listed right after it as if it was the same list.

I know CollectionView only takes one item but I wanted to know if there was a way to have two lists that can behave as one when you scroll

Thank you for your help

2

There are 2 best solutions below

3
On BEST ANSWER

you can use 2 StackLayout right after each other and set BindableLayout like this :

<StackLayout Orientation="Horizontal">
      <StackLayout Orientation="Horizontal" BindableLayout.ItemsSource="{Binding CategoryItemSource}">
          <BindableLayout.ItemTemplate>
              <DataTemplate>
                    <!--CategoryItemTemplate-->
                </DataTemplate>
          </BindableLayout.ItemTemplate>
      </StackLayout>
      <StackLayout Orientation="Horizontal" BindableLayout.ItemsSource="{Binding SubCategoryItemSource}">
          <BindableLayout.ItemTemplate>
              <DataTemplate>
                  <!--SubCategoryItemTemplate-->
              </DataTemplate>
          </BindableLayout.ItemTemplate>
      </StackLayout>
    </StackLayout>

in this case first stacklayout show you category items and second stacklayout show subcategory just right after that

0
On

two possible solutions :

1- Use inheritance to make both Category and SubCategory share a commune Parent then build an index where you make sure that each element is listed after his parent.

2- use a nested collection view to display subcategories for each category.