Skip to content
Permalink
Browse files

Feature: ReactiveRecyclerViewAdapter.GetItemViewType now passed view …

…model type (#2051)

* Added new method to provide the ViewModel and position Id
Also guarded against possible null exceptions with the lists by trying to get the ViewModel at a position
  • Loading branch information...
clintonrocksmith authored and glennawatson committed May 26, 2019
1 parent ccf305b commit 090e6d46a2fc19f56916fe6c0d36d1301f8371dd
Showing with 24 additions and 1 deletion.
  1. +24 −1 src/ReactiveUI.AndroidSupport/ReactiveRecyclerViewAdapter.cs
@@ -52,10 +52,28 @@ protected ReactiveRecyclerViewAdapter(IObservable<IChangeSet<TViewModel>> backin
/// <inheritdoc/>
public override int ItemCount => _list.Count;

/// <inheritdoc/>
public override int GetItemViewType(int position)
{
return GetItemViewType(position, GetViewModelByPosition(position));
}

/// <summary>
/// Determine the View that will be used/re-used in lists where
/// the list contains different cell designs.
/// </summary>
/// <param name="position">The position of the current view in the list.</param>
/// <param name="viewModel">The ViewModel associated with the current View.</param>
/// <returns>An ID to be used in OnCreateViewHolder.</returns>
public int GetItemViewType(int position, TViewModel viewModel)
{
return base.GetItemViewType(position);
}

/// <inheritdoc/>
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
((IViewFor)holder).ViewModel = _list.Items.ElementAt(position);
((IViewFor)holder).ViewModel = GetViewModelByPosition(position);
}

/// <inheritdoc/>
@@ -70,6 +88,11 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

private TViewModel GetViewModelByPosition(int position)
{
return position > _list.Count ? null : _list.Items.ElementAt(position);
}

private void UpdateBindings(Change<TViewModel> change)
{
switch (change.Reason)

0 comments on commit 090e6d4

Please sign in to comment.
You can’t perform that action at this time.