2014-10-03 50 views
0

我有一个MvxListView,它具有基于我的域模型中值的不同列表项模板。在每个模板中,都有一个按钮在点击事件中绑定到视图模型。我使用MvxAdapter类来根据域模型值返回正确的模板,这确实可以很好地工作。但是,由于某种原因模板按钮单击事件不会传播到视图模型。我在同一个视图上使用其他按钮并绑定到他们的点击事件就好了。MvxAdapter单击不触发事件

我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" /> 

一个正在使用的两个模板:

<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" /> 

我的,基于模型值选择模板的自定义适配器类:

 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); 
     } 
    } 

在我的视图模型中,我使用命令模式设置了事件侦听器。

public ICommand DeleteMessageCommand 
    { 
     get 
     { 
      _deleteMessageCommand = _deleteMessageCommand ?? new MvxCommand(HandleDeleteMessage); 
      return _deleteMessageCommand; 
     } 
    } 

    private void HandleDeleteMessage() 
    { 

    } 

回答

1

我发现了一个解决方案,使用MvxMessage并在我的视图模型中注册该消息。我遵循Kiliman的示例代码,这是从另一个链接stackoverflow post here

+0

很酷。我只是想指出你的职位。 – Kiliman 2014-10-03 17:41:55