New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Section indexes when FastScrolled is enabled #850

Merged
merged 4 commits into from Apr 6, 2017
Jump to file or symbol
Failed to load files and symbols.
+77 −7
Diff settings

Always

Just for now

@@ -12,7 +12,7 @@
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 @@ internal sealed class ListViewAdapter : CellAdapter
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 @@ internal sealed class ListViewAdapter : CellAdapter
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,78 @@ enum CellType
Header
}
void InvalidateCount()
protected virtual void InvalidateCount()
{
_listCount = -1;
}
}
internal class GroupedListViewAdapter : ListViewAdapter, ISectionIndexer

This comment has been minimized.

@rmarinho

rmarinho Apr 6, 2017

Member

Can we move this to other class?!

@rmarinho

rmarinho Apr 6, 2017

Member

Can we move this to other class?!

{
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)
{
Setup ();
return Sections [sectionIndex].Start;
}
public int GetSectionForPosition (int position)
{
Setup ();
foreach (var section in Sections) {
if (section.Start >= position && section.End <= position)
return section.Index;
}
return 0;
}
public Java.Lang.Object [] GetSections ()
{
Setup ();

This comment has been minimized.

@rmarinho

rmarinho Apr 6, 2017

Member

Need to fix the code style, we don't use spaces here

@rmarinho

rmarinho Apr 6, 2017

Member

Need to fix the code style, we don't use spaces here

return nativeSections;
}
void Setup ()
{
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;
}
}
}
@@ -141,7 +141,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
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 @@ void UpdateIsSwipeToRefreshEnabled()
void UpdateFastScrollEnabled()
{
if (Control != null)
Control.FastScrollEnabled = Element.OnThisPlatform().IsFastScrollEnabled();
if (Control != null) {

This comment has been minimized.

@rmarinho

rmarinho Apr 6, 2017

Member

no need for change here

@rmarinho

rmarinho Apr 6, 2017

Member

no need for change here

Control.FastScrollEnabled = Element.OnThisPlatform ().IsFastScrollEnabled ();
}
}
internal class Container : ViewGroup
ProTip! Use n and p to navigate between commits in a pull request.