2014-10-03 54 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