Reputation: 989
I'm using the following code as described in this thread to execute an event when a LinearLayout is clicked in ListView. It used to work perfectly but after update to MvvmCross 5, it does not work anymore because the mvxDataConsumer becomes null when parsed to IMvxDataConsumer and I can't figure out the reason why?
public class MvxClickableLinearLayout : MvxLinearLayout
{
public MvxClickableLinearLayout(Context context, IAttributeSet attrs)
: this(context, attrs, new MvxClickableLinearLayoutAdapter(context))
{
}
public MvxClickableLinearLayout(Context context, IAttributeSet attrs, MvxClickableLinearLayoutAdapter adapter)
: base(context, attrs, adapter)
{
var mvxClickableLinearLayoutAdapter = Adapter as MvxClickableLinearLayoutAdapter;
if (mvxClickableLinearLayoutAdapter != null)
{
mvxClickableLinearLayoutAdapter.OnItemClick = OnItemClick;
}
}
public ICommand ItemClick { get; set; }
public void OnItemClick(object item)
{
if (ItemClick != null && ItemClick.CanExecute(item))
{
ItemClick.Execute(item);
}
}
}
public class MvxClickableLinearLayoutAdapter : MvxAdapterWithChangedEvent, View.IOnClickListener
{
public delegate void ItemClickDelegate(object item);
public ItemClickDelegate OnItemClick;
public MvxClickableLinearLayoutAdapter(Context context)
: base(context)
{
}
public void OnClick(View view)
{
var mvxDataConsumer = view as IMvxDataConsumer;
if (mvxDataConsumer != null && OnItemClick != null)
{
OnItemClick(mvxDataConsumer.DataContext);
}
}
protected override View GetView(int position, View convertView, ViewGroup parent, int templateId)
{
View view = base.GetView(position, convertView, parent, templateId);
view.SetOnClickListener(this);
return view;
}
}
axml:
<Controls.MvxClickableLinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="ItemsSource Items;ItemClick GoToCommand"
local:MvxItemTemplate="@layout/main_list_item" />
Upvotes: 2
Views: 307
Reputation: 474
In your OnClick
method in MvxClickableLinearLayoutAdapter
class, change:
var mvxDataConsumer = view as IMvxDataConsumer;
to:
var mvxDataConsumer = view?.Tag as IMvxDataConsumer;
It should work now. In MvvmCross 5, it is now a view holder, not the view itself.
Upvotes: 1
Reputation: 435
Had the same issue myself and couldn't get to the bottom of it, so here's my work-around. Set the Tag of each View to it's position in the GetView that we have already overidden, then use the Tag to get the item from the adapter in the Click:
protected override Android.Views.View GetView(int position, Android.Views.View convertView, Android.Views.ViewGroup parent, int templateId)
{
var view = base.GetView(position, convertView, parent, templateId);
view.SetOnClickListener(this);
view.Tag = position;
return view;
}
#region IOnClickListener Members
public void OnClick(Android.Views.View v)
{
var id = (int)v.Tag;
var item = this.GetRawItem(id);
if (item != null && OnItemClick != null)
OnItemClick(item);
// Old Code - no longer working
// IMvxDataConsumer dataConsumer = v as IMvxDataConsumer;
// if (dataConsumer != null && OnItemClick != null)
// OnItemClick(dataConsumer.DataContext);
}
#endregion
Upvotes: 1