Skip to content

Commit

Permalink
Merge pull request #1 from gianlucaparadise/recycler-getItemViewType-…
Browse files Browse the repository at this point in the history
…handling

Recycler get item view type handling
  • Loading branch information
gianlucaparadise committed Mar 19, 2018
2 parents 1f67dfb + 9511daa commit acbe84b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,22 @@ public static class ExtensionsAndroid2
/// <param name="bindViewHolderDelegate">A delegate to the method used to bind the view to the corresponding item.</param>
/// <param name="createViewHolderDelegate">A delegate to the method used to create the view for the corresponding item.</param>
/// <param name="clickCallback">An optional delegate to a method called when an item is clicked or tapped.</param>
/// <param name="getItemViewType">A delegate to the method used to decide the view type id.</param>
/// <returns>The created <see cref="ObservableRecyclerAdapter{TItem, THolder}"/>.</returns>
public static ObservableRecyclerAdapter<TItem, THolder> GetRecyclerAdapter<TItem, THolder>(
this IList<TItem> list,
Action<THolder, TItem, int> bindViewHolderDelegate,
Func<ViewGroup, int, THolder> createViewHolderDelegate,
Action<int, View, int, View> clickCallback = null)
Action<int, View, int, View> clickCallback = null,
Func<int, int> getItemViewType = null)
where THolder : RecyclerView.ViewHolder
{
return new ObservableRecyclerAdapter<TItem, THolder>
{
DataSource = list,
BindViewHolderDelegate = bindViewHolderDelegate,
CreateViewHolderDelegate = createViewHolderDelegate,
GetItemViewTypeDelegate = getItemViewType,
ClickCallback = clickCallback
};
}
Expand Down Expand Up @@ -82,4 +85,4 @@ public static class ExtensionsAndroid2
};
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ****************************************************************************
// <copyright file="ObservableRecyclerAdapter.cs" company="GalaSoft Laurent Bugnion">
// Copyright © GalaSoft Laurent Bugnion 2009-2016
// Copyright © GalaSoft Laurent Bugnion 2009-2016
// </copyright>
// ****************************************************************************
// <author>Laurent Bugnion</author>
Expand Down Expand Up @@ -93,6 +93,16 @@ public int CellLayoutId
set;
}

/// <summary>
/// A delegate to a method taking an item's position and returning the view type.
/// If null, defult view type will always be 0.
/// </summary>
public Func<int, int> GetItemViewTypeDelegate
{
get;
set;
}

/// <summary>
/// The data source of this list adapter.
/// </summary>
Expand Down Expand Up @@ -186,6 +196,23 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi

BindViewHolderDelegate((THolder)holder, _dataSource[position], position);
}

/// <summary>
/// Gets the type of the item view and it will be passed to OnCreateViewHolder method
/// to handle multiple layout based on position.
/// </summary>
/// <returns>The item view type.</returns>
/// <param name="position">Position.</param>
public override int GetItemViewType(int position)
{
if (GetItemViewTypeDelegate == null)
{
return 0;
}

// No ViewHolderType specified --> Call the delegate
return GetItemViewTypeDelegate(position);
}

/// <summary>
/// Called when the View should be created.
Expand Down Expand Up @@ -335,4 +362,4 @@ private void RaiseSelectionChanged()
/// </summary>
public event EventHandler SelectionChanged;
}
}
}

0 comments on commit acbe84b

Please sign in to comment.