Hi.
I have create a ListView with CachingStrategy=RecycleElementAndDataTemplate. I use DataTemplateSelector with 3 templates. When I set CachingStrategy=RecycleElement my templates are changing as well. But with CachingStrategy=RecycleElementAndDataTemplate templates are not changing.
<ListView ItemsSource="{Binding GroupedItems, Mode=OneWay}" SelectionMode="Single" HasUnevenRows="True" CachingStrategy="RecycleElementAndDataTemplate" IsGroupingEnabled="True" GroupDisplayBinding="{Binding Key, Mode=OneWay}" GroupShortNameBinding="{Binding Key, Mode=OneWay}" ItemTapped="OnItemTapped"> <ListView.ItemTemplate> <datatemplateselectors:RowDocumentTemplateSelector/> </ListView.ItemTemplate> <ListView.GroupHeaderTemplate> <DataTemplate x:DataType="{x:Null}"> <ViewCell> <StackLayout VerticalOptions="FillAndExpand" Margin="0" Padding="10, 5, 10, 5"> <Label Text="{Binding Key, Mode=OneWay}" Style="{DynamicResource HeaderLabelStyle}" FontAttributes="Bold" VerticalOptions="Center"/> </StackLayout> </ViewCell> </DataTemplate> </ListView.GroupHeaderTemplate> </ListView> public abstract class DocumentTemplateSelector : DataTemplateSelector { public DataTemplate DepartmentTemplate { get; set; } public DataTemplate SmallDocumentTemplate { get; set; } public DataTemplate BigDocumentTemplate { get; set; } public DocumentTemplateSelector() { this.DepartmentTemplate = new DataTemplate(typeof(DepartmentCell)); this.SmallDocumentTemplate = new DataTemplate(typeof(SmallDocumentCell)); this.BigDocumentTemplate = new DataTemplate(typeof(BigDocumentCell)); } protected override DataTemplate OnSelectTemplate(object item, BindableObject container) { DocumentListItemViewModel viewModel = (DocumentListItemViewModel)item; if (viewModel.IsDepartment) { return this.DepartmentTemplate; } if (container.BindingContext is DocumentListViewModel listViewModel) { if (listViewModel.FullListRoot) { return this.BigDocumentTemplate; } } return this.SmallDocumentTemplate; } } public class RowDocumentTemplateSelector : DocumentTemplateSelector { }
What am I missing? How to use RecycleElementAndDataTemplate with DataTemplateSelector properly?
Answers
The
RetainElement
caching strategy specifies that the ListView will generate a cell for each item in the list, and is the default ListView behavior. It should be used in the following circumstances:And the
RecycleElementAndDataTemplate
caching strategy builds on the RecycleElement caching strategy by additionally ensuring that when a ListView uses aDataTemplateSelector
to select a DataTemplate, DataTemplates are cached by the type of item in the list. Therefore, DataTemplates are selected once per item type, instead of once per item instance.And we should notice that:
For more details about this, you can check: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/performance#caching-strategy
It is different for us to reproduce this problem according to your code snippets, what's the
DocumentListItemViewModel
,DepartmentCell
andSmallDocumentCell
? Could you please post a basic demo to github or onedriver so that we can test on our side?Xamarin forums are migrating to a new home on Microsoft Q&A!
We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
For more information, please refer to this sticky post.