【问题标题】:MvxAdapter Click Events Not FiringMvxAdapter 点击事件未触发
【发布时间】:2014-10-03 16:26:06
【问题描述】:

我有一个 MvxListView,它根据我的域模型中的值具有不同的列表项模板。在每个模板中都有一个按钮,它在点击事件上绑定到视图模型。我使用 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()
    {

    }

【问题讨论】:

    标签: mvvm android-listview mvvmcross


    【解决方案1】:

    我通过使用 MvxMessage 并在我的视图模型中注册该消息找到了解决方案。我遵循来自Kiliman 的示例代码,它是来自另一个stackoverflow post here 的链接

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多