ItemClick for MvxLinearLayout? MvvmCross 5

时间:2017-06-01 02:13:14

标签: xamarin.android mvvmcross

我在{{3}}线程中使用以下代码在ListView中单击LinearLayout时执行事件。它曾经很好地工作但是在更新到MvvmCross 5之后,它不再起作用了,因为mvxDataConsumer在解析为IMvxDataConsumer时变为null而我无法找出原因?

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

2 个答案:

答案 0 :(得分:1)

我自己有同样的问题,无法深究它,所以这是我的解决方案。将每个View的Tag添加到我们已经覆盖的GetView中的位置,然后使用Tag从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

答案 1 :(得分:1)

std::vector课程的OnClick方法中,更改: MvxClickableLinearLayoutAdapter 至: var mvxDataConsumer = view as IMvxDataConsumer;

现在应该可以了。在MvvmCross 5中,它现在是一个视图持有者,而不是视图本身。