diff options
author | James Clancey <james.clancey@gmail.com> | 2017-04-06 09:54:00 -0800 |
---|---|---|
committer | Rui Marinho <me@ruimarinho.net> | 2017-04-06 18:54:00 +0100 |
commit | 3d9d1d3d222c8b4edaa1cf28e7ad102e8e341eed (patch) | |
tree | 3253141a823109090922b87c69d61088f3f5ccc2 /Xamarin.Forms.Platform.Android | |
parent | 425fafb05723a299a5eaa0f6d801f87bdf7141fa (diff) | |
download | xamarin-forms-3d9d1d3d222c8b4edaa1cf28e7ad102e8e341eed.tar.gz xamarin-forms-3d9d1d3d222c8b4edaa1cf28e7ad102e8e341eed.tar.bz2 xamarin-forms-3d9d1d3d222c8b4edaa1cf28e7ad102e8e341eed.zip |
Added Section indexes when FastScrolled is enabled (#850)
* Added Section indexes when FastScrolled is enabled
* Moved the GroupListAdapater to its own file
* Renamed Setup to ValidateSectionData
Diffstat (limited to 'Xamarin.Forms.Platform.Android')
4 files changed, 92 insertions, 7 deletions
diff --git a/Xamarin.Forms.Platform.Android/Renderers/GroupedListViewAdapter.cs b/Xamarin.Forms.Platform.Android/Renderers/GroupedListViewAdapter.cs new file mode 100644 index 00000000..313ded24 --- /dev/null +++ b/Xamarin.Forms.Platform.Android/Renderers/GroupedListViewAdapter.cs @@ -0,0 +1,82 @@ +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; + } + } +} diff --git a/Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs b/Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs index 5c23fd2e..4c9d2311 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs @@ -12,7 +12,7 @@ using Xamarin.Forms.Internals; namespace Xamarin.Forms.Platform.Android { - internal sealed class ListViewAdapter : CellAdapter + internal class ListViewAdapter : CellAdapter { const int DefaultGroupHeaderTemplateId = 0; const int DefaultItemTemplateId = 1; @@ -22,7 +22,7 @@ namespace Xamarin.Forms.Platform.Android 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 @@ -34,7 +34,7 @@ namespace Xamarin.Forms.Platform.Android 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) { @@ -595,9 +595,10 @@ namespace Xamarin.Forms.Platform.Android Header } - void InvalidateCount() + protected virtual void InvalidateCount() { _listCount = -1; } } + }
\ No newline at end of file diff --git a/Xamarin.Forms.Platform.Android/Renderers/ListViewRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/ListViewRenderer.cs index bd241c1a..f15b665a 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/ListViewRenderer.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/ListViewRenderer.cs @@ -141,7 +141,7 @@ namespace Xamarin.Forms.Platform.Android 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; @@ -335,8 +335,9 @@ namespace Xamarin.Forms.Platform.Android void UpdateFastScrollEnabled() { - if (Control != null) - Control.FastScrollEnabled = Element.OnThisPlatform().IsFastScrollEnabled(); + if (Control != null) { + Control.FastScrollEnabled = Element.OnThisPlatform ().IsFastScrollEnabled (); + } } internal class Container : ViewGroup diff --git a/Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj b/Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj index e57a501d..bf5344ff 100644 --- a/Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj +++ b/Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj @@ -255,6 +255,7 @@ <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> |