Xamarin CollectionView 简介Xamarin.Forms CollectionView Introduction

下载示例 下载示例Download Sample Download the sample

CollectionView 是使用不同布局规范显示数据列表的视图。CollectionView is a view for presenting lists of data using different layout specifications. 它旨在为ListView提供更灵活、更高性能的替代方法。It aims to provide a more flexible, and performant alternative to ListView. 例如,下面的屏幕截图显示了一个使用两个列垂直网格的 CollectionView,这允许多个选择:For example, the following screenshots show a CollectionView that uses a two column vertical grid, and which allows multiple selection:

IOS 和 Android 上的 CollectionView 垂直网格布局的屏幕截图Screenshot of a CollectionView vertical grid layout, on iOS and Android

可从 Xamarin 4.3 获取CollectionViewCollectionView is available from Xamarin.Forms 4.3.


CollectionView在 IOS 和 Android 上可用,但在通用 Windows 平台上仅部分可用CollectionView is available on iOS and Android, but is only partially available on the Universal Windows Platform.

CollectionView 和 ListView 差异CollectionView and ListView differences

尽管CollectionViewListView api 类似,但也有一些明显的区别:While the CollectionView and ListView APIs are similar, there are some notable differences:

从 ListView 移到 CollectionViewMove from ListView to CollectionView

现有 Xamarin 中的ListView实现。窗体实现可以通过下表的帮助迁移到CollectionView实现:ListView implementations in existing Xamarin.Forms implementations can be migrated to CollectionView implementations with the help of the following table:

概念Concept ListView APIListView API CollectionViewCollectionView
数据Data ItemsSource 通过设置 ItemsSource 属性,使用数据填充CollectionViewA CollectionView is populated with data by setting its ItemsSource property. 有关详细信息,请参阅使用数据填充 CollectionViewFor more information, see Populate a CollectionView with data.
项外观Item appearance ItemTemplate 可以通过将 ItemTemplate 属性设置为DataTemplate来定义CollectionView中每个项的外观。The appearance of each item in a CollectionView can be defined by setting the ItemTemplate property to a DataTemplate. 有关详细信息,请参阅定义项外观For more information, see Define item appearance.
单元Cells TextCell中, ImageCell中, ViewCellTextCell, ImageCell, ViewCell CollectionView没有单元的概念。CollectionView has no concept of cells. 相反,数据模板用于定义列表中每个数据项的外观。Instead, a data template is used to define the appearance of each item of data in the list.
行分隔符Row separators SeparatorColorSeparatorVisibilitySeparatorColor, SeparatorVisibility CollectionView不包含内置分隔符。CollectionView does not include built-in separators. 如果需要,可在项模板中提供这些项。These can be provided, if desired, in the item template.
选择Selection SelectionModeSelectedItemSelectionMode, SelectedItem CollectionView支持单个和多个选择。CollectionView supports single and multiple selection. 有关详细信息,请参阅Xamarin CollectionView 选择For more information, see Xamarin.Forms CollectionView Selection.
行高Row height HasUnevenRowsRowHeightHasUnevenRows, RowHeight CollectionView 中,每个项的行高由 ItemSizingStrategy 属性确定。In a CollectionView, the row height of each item is determined by the ItemSizingStrategy property. 有关详细信息,请参阅项大小调整For more information, see Item sizing.
缓存Caching CachingStrategy CollectionView自动使用基础本机控件提供的虚拟化。CollectionView automatically uses the virtualization provided by the underlying native controls.
页眉和页脚Headers and footers HeaderHeaderElementHeaderTemplateFooterFooterElementFooterTemplateHeader, HeaderElement, HeaderTemplate, Footer, FooterElement, FooterTemplate CollectionView可以通过 "Header"、"Footer"、"HeaderTemplate" 和 "FooterTemplate" 属性来显示通过列表中的项滚动的页眉和页脚。CollectionView can present a header and footer that scroll with the items in the list, via the Header, Footer, HeaderTemplate, and FooterTemplate properties. 有关详细信息,请参阅页眉和页脚For more information, see Headers and footers.
分组Grouping GroupDisplayBinding, GroupHeaderTemplate, GroupShortNameBinding, IsGroupingEnabledGroupDisplayBinding, GroupHeaderTemplate, GroupShortNameBinding, IsGroupingEnabled CollectionView通过将其 IsGrouped 属性设置为 true,来显示正确分组的数据。CollectionView displays correctly grouped data by setting its IsGrouped property to true. 可以通过将 GroupHeaderTemplateGroupFooterTemplate 属性设置为DataTemplate对象来自定义组头和组尾。Group headers and group footers can be customized by setting the GroupHeaderTemplate and GroupFooterTemplate properties to DataTemplate objects. 有关详细信息,请参阅Xamarin CollectionView 分组For more information, see Xamarin.Forms CollectionView Grouping.
请求刷新Pull to refresh IsPullToRefreshEnabled, IsRefreshing, RefreshAllowed, RefreshCommand, RefreshControlColor, BeginRefresh(), EndRefresh()IsPullToRefreshEnabled, IsRefreshing, RefreshAllowed, RefreshCommand, RefreshControlColor, BeginRefresh(), EndRefresh() 通过将CollectionView设置为 RefreshView 的子级,支持拉取到刷新功能。Pull to refresh functionality is supported by setting a CollectionView as the child of a RefreshView. 有关详细信息,请参阅请求刷新For more information, see Pull to refresh.
上下文操作Context actions ContextActions CollectionView中当前不支持上下文操作,但会在将来的版本中添加。Context actions are currently unsupported in CollectionView, but will be added in a future release.
滚动Scrolling ScrollTo() CollectionView定义 ScrollTo 方法,这些方法会将项滚动到视图中。CollectionView defines ScrollTo methods, that scroll items into view. 有关详细信息,请参阅滚动For more information, see Scrolling.