Skip to content

Commit

Permalink
Prototypical Cell Cache for IsEnabled testing (xamarin#1179)
Browse files Browse the repository at this point in the history
  • Loading branch information
kingces95 authored Oct 9, 2017
1 parent 12ebce7 commit 57c8b36
Showing 1 changed file with 66 additions and 12 deletions.
78 changes: 66 additions & 12 deletions Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using AView = Android.Views.View;
using AListView = Android.Widget.ListView;
using Xamarin.Forms.Internals;
using System.Collections;

namespace Xamarin.Forms.Platform.Android
{
Expand All @@ -32,7 +33,7 @@ internal class ListViewAdapter : CellAdapter
// To prevent a conflict in the event that a ListView supports both templates and non-templates, we will start the DataTemplate key at 2.

int _listCount = -1; // -1 we need to get count from the list
Cell _enabledCheckCell;
Dictionary<object, Cell> _prototypicalCellByTypeOrDataTemplate;

bool _fromNative;
AView _lastSelected;
Expand All @@ -46,6 +47,7 @@ public ListViewAdapter(Context context, AListView realListView, ListView listVie
_context = context;
_realListView = realListView;
_listView = listView;
_prototypicalCellByTypeOrDataTemplate = new Dictionary<object, Cell>();

if (listView.SelectedItem != null)
SelectItem(listView.SelectedItem);
Expand Down Expand Up @@ -309,6 +311,68 @@ public override AView GetView(int position, AView convertView, ViewGroup parent)
return layout;
}

internal void InvalidatePrototypicalCellCache()
{
_prototypicalCellByTypeOrDataTemplate.Clear();
}

internal ITemplatedItemsList<Cell> GetTemplatedItemsListForPath(int indexPath)
{
var templatedItems = TemplatedItemsView.TemplatedItems;
if (_listView.IsGroupingEnabled)
templatedItems = (ITemplatedItemsList<Cell>)((IList)templatedItems)[indexPath];

return templatedItems;
}

internal DataTemplate GetDataTemplateForPath(int indexPath)
{
var templatedItemsList = GetTemplatedItemsListForPath(indexPath);
var item = templatedItemsList.ListProxy[indexPath];
return templatedItemsList.SelectDataTemplate(item);
}

internal Type GetItemTypeForPath(int indexPath)
{
var templatedItemsList = GetTemplatedItemsListForPath(indexPath);
var item = templatedItemsList.ListProxy[indexPath];
return item.GetType();
}

internal Cell GetCellForPath(int indexPath)
{
var templatedItemsList = GetTemplatedItemsListForPath(indexPath);
var cell = templatedItemsList[indexPath];
return cell;
}

internal Cell GetPrototypicalCell(int indexPath)
{
var itemTypeOrDataTemplate = default(object);

var cachingStrategy = _listView.CachingStrategy;
if (cachingStrategy == ListViewCachingStrategy.RecycleElement)
itemTypeOrDataTemplate = GetDataTemplateForPath(indexPath);

else if (cachingStrategy == ListViewCachingStrategy.RecycleElementAndDataTemplate)
itemTypeOrDataTemplate = GetItemTypeForPath(indexPath);

else // ListViewCachingStrategy.RetainElement
return GetCellForPosition(indexPath);

Cell protoCell;
if (!_prototypicalCellByTypeOrDataTemplate.TryGetValue(itemTypeOrDataTemplate, out protoCell))
{
// cache prototypical cell by item type; Items of the same Type share
// the same DataTemplate (this is enforced by RecycleElementAndDataTemplate)
protoCell = GetCellForPosition(indexPath);
_prototypicalCellByTypeOrDataTemplate[itemTypeOrDataTemplate] = protoCell;
}

var templatedItems = GetTemplatedItemsListForPath(indexPath);
return templatedItems.UpdateContent(protoCell, indexPath);
}

public override bool IsEnabled(int position)
{
ListView list = _listView;
Expand All @@ -321,17 +385,7 @@ public override bool IsEnabled(int position)
return leftOver > 0;
}

var strategy = ((IListViewController)list).CachingStrategy;
if ((strategy & ListViewCachingStrategy.RecycleElement) != 0)
{
if (_enabledCheckCell == null)
_enabledCheckCell = GetCellForPosition(position);
else
templatedItemsView.TemplatedItems.UpdateContent(_enabledCheckCell, position);
return _enabledCheckCell.IsEnabled;
}

Cell item = GetCellForPosition(position);
Cell item = GetPrototypicalCell(position);
return item.IsEnabled;
}

Expand Down

0 comments on commit 57c8b36

Please sign in to comment.