Reputation: 271
I have a MvxListView that has different list item templates based on values in my domain model. In each template there is a button that is bound to the view model on the click event. I used the MvxAdapter class to return the correct template based on the domain model value and this does work perfectly. However, for some reason the template button click event is not propagating to the view model. I use other buttons on the same view and bind to their click event just fine.
My MvxList:
<Mvx.MvxListView
local:MvxBind="ItemsSource MessageItems; ItemClick TextMessageSelectedCommand"
local:MvxItemTemplate="@layout/template_message_item_inbound"
android:id="@+id/MessageList"
android:divider="@drawable/list_divider_selector"
android:choiceMode="singleChoice"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_above="@+id/input"
style="@style/MessageListStyle" />
One of the two templates that are used:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="BP"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="65dp"
android:layout_height="65dp"
android:id="@+id/txtInitials"
local:MvxBind="Text From, Converter=NameToInitials"
android:background="@drawable/message_badge_inbound"
android:layout_gravity="center"
android:gravity="center"
style="@style/MessageUserBadge" />
<TextView
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/txtInitials"
android:background="@drawable/speech_bubble_inbound"
local:MvxBind="Text Message"
android:id="@+id/txtMessage"
android:layout_marginRight="65dp" />
<TextView
android:text="12:53 pm"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:layout_below="@+id/txtInitials"
local:MvxBind="Text Time, Converter=DateToString, ConverterParameter='t'"
android:id="@+id/txtTime"
style="@style/MessageTime"
android:gravity="center" />
<ImageButton
android:text="Button"
local:MvxBind="Click DeleteMessageCommand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnDeleteMessage"
android:src="@drawable/ic_action_delete"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:tag="InboundDeleteButton" />
My custom adapter class that selects the template based on model values:
public class CustomAdapter : MvxAdapter
{
public CustomAdapter(Context context, IMvxAndroidBindingContext bindingContext)
: base(context, bindingContext)
{
}
public override int GetItemViewType(int position)
{
var item = GetRawItem(position);
if (item is TextMessage && (item as TextMessage).Direction == MessageDirection.Inbound)
return 0;
return 1;
}
public override int ViewTypeCount
{
get { return 2; }
}
protected override View GetBindableView(View convertView, object source, int templateId)
{
if (source is TextMessage && (source as TextMessage).Direction == MessageDirection.Inbound)
{
templateId = Resource.Layout.template_message_item_inbound;
}
else if (source is TextMessage && (source as TextMessage).Direction == MessageDirection.Outbound)
{
templateId = Resource.Layout.template_message_item_outbound;
}
return base.GetBindableView(convertView, source, templateId);
}
}
In my view model I setup an event listener using the command pattern.
public ICommand DeleteMessageCommand
{
get
{
_deleteMessageCommand = _deleteMessageCommand ?? new MvxCommand(HandleDeleteMessage);
return _deleteMessageCommand;
}
}
private void HandleDeleteMessage()
{
}
Upvotes: 0
Views: 260
Reputation: 271
I found a solution by using a MvxMessage and registering for that message in my view model. I followed the example code from Kiliman which was link from another stackoverflow post here
Upvotes: 1