Added Section indexes when FastScrolled is enabled (#850)
authorJames Clancey <james.clancey@gmail.com>
Thu, 6 Apr 2017 17:54:00 +0000 (09:54 -0800)
committerRui Marinho <me@ruimarinho.net>
Thu, 6 Apr 2017 17:54:00 +0000 (18:54 +0100)
* Added Section indexes when FastScrolled is enabled

* Moved the GroupListAdapater to its own file

* Renamed Setup to ValidateSectionData

Xamarin.Forms.Platform.Android/Renderers/GroupedListViewAdapter.cs [new file with mode: 0644]
Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs
Xamarin.Forms.Platform.Android/Renderers/ListViewRenderer.cs
Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj

diff --git a/Xamarin.Forms.Platform.Android/Renderers/GroupedListViewAdapter.cs b/Xamarin.Forms.Platform.Android/Renderers/GroupedListViewAdapter.cs
new file mode 100644 (file)
index 0000000..313ded2
--- /dev/null
@@ -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;
+               }
+       }
+}
index 5c23fd2..4c9d231 100644 (file)
@@ -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
index bd241c1..f15b665 100644 (file)
@@ -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
index e57a501..bf5344f 100644 (file)
     <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>