--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Android.Content;
+using Android.Widget;
+using AListView = Android.Widget.ListView;
+
+namespace Xamarin.Forms.Platform.Android
+{
+
+ internal class GroupedListViewAdapter : ListViewAdapter, ISectionIndexer
+ {
+ class SectionData
+ {
+ public int Index { get; set; }
+ public int Length { get; set; }
+ public int Start { get; set; }
+ public int End => Start + Length;
+ }
+ public GroupedListViewAdapter(Context context, AListView realListView, ListView listView) : base(context, realListView, listView)
+ {
+
+ }
+ bool sectionDataValid = false;
+
+ SectionData[] Sections;
+ Java.Lang.Object[] nativeSections;
+ public int GetPositionForSection(int sectionIndex)
+ {
+ ValidateSectionData();
+ return Sections[sectionIndex].Start;
+ }
+
+ public int GetSectionForPosition(int position)
+ {
+ ValidateSectionData();
+ foreach (var section in Sections)
+ {
+ if (section.Start >= position && section.End <= position)
+ return section.Index;
+ }
+ return 0;
+ }
+
+ public Java.Lang.Object[] GetSections()
+ {
+ ValidateSectionData();
+ return nativeSections;
+ }
+
+ void ValidateSectionData()
+ {
+ if (sectionDataValid)
+ return;
+
+ var templatedItems = TemplatedItemsView.TemplatedItems;
+ int count = 0;
+
+ var sectionData = new List<SectionData>();
+ for (var i = 0; i < templatedItems.Count; i++)
+ {
+ var groupCount = templatedItems.GetGroup(i).Count;
+ sectionData.Add(new SectionData { Index = i, Length = groupCount, Start = count });
+ count += groupCount;
+ }
+ Sections = sectionData.ToArray();
+
+ var shortNames = templatedItems.ShortNames;
+ if (shortNames != null)
+ {
+ nativeSections = shortNames.Select(x => new Java.Lang.String(x)).ToArray();
+ }
+ sectionDataValid = true;
+ }
+
+ protected override void InvalidateCount()
+ {
+ base.InvalidateCount();
+ sectionDataValid = false;
+ }
+ }
+}
namespace Xamarin.Forms.Platform.Android
{
- internal sealed class ListViewAdapter : CellAdapter
+ internal class ListViewAdapter : CellAdapter
{
const int DefaultGroupHeaderTemplateId = 0;
const int DefaultItemTemplateId = 1;
internal static readonly BindableProperty IsSelectedProperty = BindableProperty.CreateAttached("IsSelected", typeof(bool), typeof(Cell), false);
readonly Context _context;
- readonly ListView _listView;
+ protected readonly ListView _listView;
readonly AListView _realListView;
readonly Dictionary<DataTemplate, int> _templateToId = new Dictionary<DataTemplate, int>();
int _dataTemplateIncrementer = 2; // lets start at not 0 because
WeakReference<Cell> _selectedCell;
IListViewController Controller => _listView;
- ITemplatedItemsView<Cell> TemplatedItemsView => _listView;
+ protected ITemplatedItemsView<Cell> TemplatedItemsView => _listView;
public ListViewAdapter(Context context, AListView realListView, ListView listView) : base(context)
{
Header
}
- void InvalidateCount()
+ protected virtual void InvalidateCount()
{
_listCount = -1;
}
}
+
}
\ No newline at end of file
nativeListView.Focusable = false;
nativeListView.DescendantFocusability = DescendantFocusability.AfterDescendants;
nativeListView.OnFocusChangeListener = this;
- nativeListView.Adapter = _adapter = new ListViewAdapter(Context, nativeListView, e.NewElement);
+ nativeListView.Adapter = _adapter = e.NewElement.IsGroupingEnabled && e.NewElement.OnThisPlatform ().IsFastScrollEnabled () ? new GroupedListViewAdapter (Context, nativeListView, e.NewElement) : new ListViewAdapter(Context, nativeListView, e.NewElement);
_adapter.HeaderView = _headerView;
_adapter.FooterView = _footerView;
_adapter.IsAttachedToWindow = _isAttached;
void UpdateFastScrollEnabled()
{
- if (Control != null)
- Control.FastScrollEnabled = Element.OnThisPlatform().IsFastScrollEnabled();
+ if (Control != null) {
+ Control.FastScrollEnabled = Element.OnThisPlatform ().IsFastScrollEnabled ();
+ }
}
internal class Container : ViewGroup
<Compile Include="Extensions\NativeBindingExtensions.cs" />
<Compile Include="NativeValueConverterService.cs" />
<Compile Include="NativeBindingservice.cs" />
+ <Compile Include="Renderers\GroupedListViewAdapter.cs" />
<Compile Include="FastRenderers\ImageRenderer.cs" />
<Compile Include="Extensions\ImageViewExtensions.cs" />
</ItemGroup>