Skip to content
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

Recycler get item view type handling #1

Merged
merged 2 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}
}