MvxRecyclerView Fluent API 绑定 [英] MvxRecyclerView Fluent API binding

查看:20
本文介绍了MvxRecyclerView Fluent API 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用 Fluent API 将 ItemClick 从 MvxRecyclerView(或其适配器)绑定到我的 ViewModel 上的命令.如果我将 ItemsSource 和 ItemClick 都放在 XML 中,它会起作用,所以我对这种解决方案不感兴趣.

I am unable to bind ItemClick from MvxRecyclerView (or its Adapter) to a command on my ViewModel using Fluent API. It works if I put both ItemsSource and ItemClick in the XML so I am not interested in such solution.

我用这篇文章作为一个很好的指导方针(如何使用 MvvmCross fluent API 将 RecyclerView 项目的 TextView 绑定到 Android 上其 ViewModel 的属性?),所有这些都有效,只是我无法绑定 ItemClick在 MvxRecyclerView(或适配器)到 MainViewModel 的命令,它将带我到下一个片段(ItemsSource 就像一个魅力,但它是一个属性而不是命令!).

I used this post as an excellent guideline (How to use the MvvmCross fluent API to bind a RecyclerView item's TextView to a property of its ViewModel on Android?) and all of that works except that I am unable to bind ItemClick on MvxRecyclerView (or the adapter) to a MainViewModel's command which will take me to the next fragment (ItemsSource works like a charm but its a property and not a command!).

为简洁起见,我不会复制原始帖子中的代码(如何使用 MvvmCross fluent API 将 RecyclerView 项目的 TextView 绑定到 Android 上其 ViewModel 的属性?)所以假设该帖子中的 MainViewModel 已通过命令 ShowItemCommand 进行了增强,如下所示:

For the sake of brevity, I will not be copying the code from the original post (How to use the MvvmCross fluent API to bind a RecyclerView item's TextView to a property of its ViewModel on Android?) so assume that the MainViewModel from that post has been enhanced with a command ShowItemCommand as such:

public class MainViewModel : MvxViewModel
{
    private IEnumerable<ViewModelItem> _viewModelItems;
    public IEnumerable<ViewModelItem> ViewModelItems
    {
        get { return _viewModelItems; }
        set { SetProperty(ref _viewModelItems, value); }
    }    

    public MvxCommand<ViewModelItem> ShowItemCommand
    {
        get
        {
            return new MvxCommand<ViewModelItem>(selectedItem =>
            {
                ShowViewModel<ViewModelItem>
                (new { itemId = selectedItem.Id });
            });
        }
    }
}

其他所有内容均已按照引用的帖子实施.

and everything else has been implemented as per the referenced post.

所以现在,除了 ItemsSource,我想将 MvxRecyclerView(或适配器)上的 ItemClick 连接到命令.它们可以互换的原因是 MvxRecyclerView 只是将这些命令中继到适配器.

So now, in addition to ItemsSource, I want to wire up ItemClick on the MvxRecyclerView (or the Adapter) to the command. The reason these are interchangeable is that MvxRecyclerView just relays these commands to the Adapter.

显然,这应该有效......但它没有:

Apparently, this should work...but it does not:

adapter.ItemClick = ViewModel.ShowItemCommand;

这也不起作用:

set.Bind(recyclerView).For(v => v.ItemClick).To(vm => vm.ShowItemCommand);

推荐答案

在创建自定义 MvxRecyclerViewHolder 时,您需要确保将 Click 命令分配给 ViewHolder>.这是在自定义适配器的 OnCreateViewHolder 覆盖中完成的.

When creating a custom MvxRecyclerViewHolder you need to make sure that you assign the Click command over to the ViewHolder. This is done in the OnCreateViewHolder override of your custom adapter.

自定义ViewHolder

public class MyAdapter : MvxRecyclerAdapter
{
    public MyAdapter(IMvxAndroidBindingContext bindingContext)
        : base(bindingContext)
    {
    }

    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        var itemBindingContext = new MvxAndroidBindingContext(parent.Context, this.BindingContext.LayoutInflaterHolder);
        var view = this.InflateViewForHolder(parent, viewType, itemBindingContext);

        return new MyViewHolder(view, itemBindingContext)
        {
            Click = ItemClick,
            LongClick = ItemLongClick
        };
    }
}

这篇关于MvxRecyclerView Fluent API 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆