diff --git a/Samples/Example.Droid/Fragments/ExampleRecyclerViewFragment.cs b/Samples/Example.Droid/Fragments/ExampleRecyclerViewFragment.cs index 562989a4..288cc9f2 100644 --- a/Samples/Example.Droid/Fragments/ExampleRecyclerViewFragment.cs +++ b/Samples/Example.Droid/Fragments/ExampleRecyclerViewFragment.cs @@ -1,4 +1,5 @@ using System; +using System.Windows.Input; using Android.OS; using Android.Runtime; using Android.Support.Design.Widget; @@ -9,6 +10,8 @@ using MvvmCross.Droid.Support.V7.RecyclerView; using MvvmCross.Droid.Support.V4; using Example.Core.ViewModels; +using MvvmCross.Binding.Droid.BindingContext; +using MvvmCross.Core.ViewModels; using MvvmCross.Droid.Shared.Attributes; namespace Example.Droid.Fragments @@ -26,6 +29,13 @@ public override View OnCreateView(LayoutInflater inflater, ViewGroup container, var recyclerView = view.FindViewById(Resource.Id.my_recycler_view); if (recyclerView != null) { + // User our custom adapter + // Doing this will make things break on call on + // MyCustomAdapter::OnCreateViewHolder because MyCustomAdapter::BindingContext + // is null... + var adapter = new MyCustomAdapter {ItemCustomClick = new MvxCommand(DoItemCustomClick)}; + + recyclerView.Adapter = adapter; recyclerView.HasFixedSize = true; var layoutManager = new LinearLayoutManager(Activity); recyclerView.SetLayoutManager(layoutManager); @@ -47,6 +57,12 @@ public override View OnCreateView(LayoutInflater inflater, ViewGroup container, return view; } + private void DoItemCustomClick() + { + // This is never executed because of NullReference on ViewHolder creation + Toast.MakeText(Activity, "Here I am", ToastLength.Short).Show(); + } + public override void OnDestroyView() { base.OnDestroyView(); @@ -55,5 +71,66 @@ public override void OnDestroyView() } protected override int FragmentId => Resource.Layout.fragment_example_recyclerview; + + //////////////////////////////////////////////////////////////////////// + // Let's create a custom adapter to use a custom ViewHolder (in order + // to add custom bindings for example). + //////////////////////////////////////////////////////////////////////// + public class MyCustomAdapter : MvxRecyclerAdapter + { + public ICommand ItemCustomClick { get; set; } + + public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) + { + ////////////////////////////////////////////////////////////////////////////////////// + // Here is the NullReference exception : BindingContext is null + var itemBindingContext = new MvxAndroidBindingContext(parent.Context, BindingContext.LayoutInflaterHolder); + + return new MyCustomViewHolder(InflateViewForHolder(parent, viewType, itemBindingContext), itemBindingContext) + { + Click = ItemClick, + LongClick = ItemLongClick, + CustomClick = ItemCustomClick + }; + } + + + } + } + + public class MyCustomViewHolder : MvxRecyclerViewHolder + { + public MyCustomViewHolder(View itemView, MvxAndroidBindingContext bindingContext) + : base(itemView, bindingContext) + { + } + + private bool _customClickDefined; + private ICommand _customClick; + + public ICommand CustomClick + { + get + { + return _customClick; + } + + set + { + _customClick = value; + if (null != _customClick && !_customClickDefined) + { + _customClickDefined = true; + /////////////////////////////////////////////////////////////////////////// + // Concrete sample of why I would want to customize the ViewHolder: adding + // custom click binding on a given view + /////////////////////////////////////////////////////////////////////////// + + //var view = ItemView.FindViewById(Resource.Id.MyCustomImage); + //if (null != view) + // view.CustomClickEvent = CustomClick; + } + } + } } } \ No newline at end of file