From 21156f8673cbe5d044fd583c16788459d75f0533 Mon Sep 17 00:00:00 2001 From: gianlucaparadise Date: Tue, 13 Mar 2018 11:21:19 +0100 Subject: [PATCH 1/2] Added GetItemViewTypeDelegate Added GetItemViewTypeDelegate in GetRecyclerAdapter extension method --- .../Helpers/ExtensionsAndroid2.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/GalaSoft.MvvmLight/GalaSoft.MvvmLight.AndroidSupport/Helpers/ExtensionsAndroid2.cs b/GalaSoft.MvvmLight/GalaSoft.MvvmLight.AndroidSupport/Helpers/ExtensionsAndroid2.cs index 73a5157e..1ad7fea0 100644 --- a/GalaSoft.MvvmLight/GalaSoft.MvvmLight.AndroidSupport/Helpers/ExtensionsAndroid2.cs +++ b/GalaSoft.MvvmLight/GalaSoft.MvvmLight.AndroidSupport/Helpers/ExtensionsAndroid2.cs @@ -41,12 +41,14 @@ public static class ExtensionsAndroid2 /// A delegate to the method used to bind the view to the corresponding item. /// A delegate to the method used to create the view for the corresponding item. /// An optional delegate to a method called when an item is clicked or tapped. + /// A delegate to the method used to decide the view type id. /// The created . public static ObservableRecyclerAdapter GetRecyclerAdapter( this IList list, Action bindViewHolderDelegate, Func createViewHolderDelegate, - Action clickCallback = null) + Action clickCallback = null, + Func getItemViewType = null) where THolder : RecyclerView.ViewHolder { return new ObservableRecyclerAdapter @@ -54,6 +56,7 @@ public static class ExtensionsAndroid2 DataSource = list, BindViewHolderDelegate = bindViewHolderDelegate, CreateViewHolderDelegate = createViewHolderDelegate, + GetItemViewTypeDelegate = getItemViewType, ClickCallback = clickCallback }; } @@ -82,4 +85,4 @@ public static class ExtensionsAndroid2 }; } } -} \ No newline at end of file +} From 9511daa90c238aeba151c1f6842edb7f9541edb3 Mon Sep 17 00:00:00 2001 From: gianlucaparadise Date: Tue, 13 Mar 2018 11:24:53 +0100 Subject: [PATCH 2/2] Added GetItemViewTypeDelegate Added GetItemViewTypeDelegate handling overriding GetItemViewType method --- .../Helpers/ObservableRecyclerAdapter.cs | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/GalaSoft.MvvmLight/GalaSoft.MvvmLight.AndroidSupport/Helpers/ObservableRecyclerAdapter.cs b/GalaSoft.MvvmLight/GalaSoft.MvvmLight.AndroidSupport/Helpers/ObservableRecyclerAdapter.cs index e04c89de..078447fe 100644 --- a/GalaSoft.MvvmLight/GalaSoft.MvvmLight.AndroidSupport/Helpers/ObservableRecyclerAdapter.cs +++ b/GalaSoft.MvvmLight/GalaSoft.MvvmLight.AndroidSupport/Helpers/ObservableRecyclerAdapter.cs @@ -1,6 +1,6 @@ // **************************************************************************** // -// Copyright © GalaSoft Laurent Bugnion 2009-2016 +// Copyright © GalaSoft Laurent Bugnion 2009-2016 // // **************************************************************************** // Laurent Bugnion @@ -93,6 +93,16 @@ public int CellLayoutId set; } + /// + /// A delegate to a method taking an item's position and returning the view type. + /// If null, defult view type will always be 0. + /// + public Func GetItemViewTypeDelegate + { + get; + set; + } + /// /// The data source of this list adapter. /// @@ -186,6 +196,23 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi BindViewHolderDelegate((THolder)holder, _dataSource[position], position); } + + /// + /// Gets the type of the item view and it will be passed to OnCreateViewHolder method + /// to handle multiple layout based on position. + /// + /// The item view type. + /// Position. + public override int GetItemViewType(int position) + { + if (GetItemViewTypeDelegate == null) + { + return 0; + } + + // No ViewHolderType specified --> Call the delegate + return GetItemViewTypeDelegate(position); + } /// /// Called when the View should be created. @@ -335,4 +362,4 @@ private void RaiseSelectionChanged() /// public event EventHandler SelectionChanged; } -} \ No newline at end of file +}