Skip to content

Commit

Permalink
Custom adapter sample
Browse files Browse the repository at this point in the history
  • Loading branch information
sescandell committed Apr 12, 2016
1 parent b3a0b90 commit 502f1ac
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions 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;
Expand All @@ -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
Expand All @@ -26,6 +29,13 @@ public override View OnCreateView(LayoutInflater inflater, ViewGroup container,
var recyclerView = view.FindViewById<MvxRecyclerView>(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);
Expand All @@ -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();
Expand All @@ -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<ImageView>(Resource.Id.MyCustomImage);
//if (null != view)
// view.CustomClickEvent = CustomClick;
}
}
}
}
}

0 comments on commit 502f1ac

Please sign in to comment.