有没有办法让 Xamarin.Forms 中的 CollectionView 中的 2 个不同对象一个接一个?

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

我有一个显示 Categories 对象列表的 CollectionView,如下所示:

我想在它后面列出一个子类别列表,就好像它是同一个列表一样。

我知道 CollectionView 只接受一个项目,但我想知道是否有一种方法可以让两个列表在滚动时表现得像一个列表

感谢您的帮助

您可以一个接一个地使用 2 个 StackLayout 并设置 BindableLayout 像这样:

<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>

在这种情况下,第一个 stacklayout 显示类别项目,第二个 stacklayout 紧接着显示子类别

两种可能的解决方案:

1- 使用继承使 Category 和 SubCategory 共享一个公社 Parent 然后建立一个索引,确保每个元素都列在他的 parent 之后。

2- 使用嵌套集合视图显示每个类别的子类别。