ecyclerView项目的TextView绑定到其ViewMo

ecyclerView项目的TextView绑定到其ViewMo

本文介绍了如何在Android上使用MvvmCross fluent API将RecyclerView项目的TextView绑定到其ViewModel的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Xamarin Android 项目中使用 MvvmCross .我有一个MvxActivity和一个MvxRecyclerView,我已经在其布局文件中分配了一个项目模板.

I am using MvvmCross in my Xamarin Android project. I have an MvxActivity with an MvxRecyclerView, that I have assigned an item template in its layout file.

<MvxRecyclerView
    android:id="@+id/my_recycler_view"
    local:MvxItemTemplate="@layout/item_recycler_view" />

ViewModel非常简单,它仅包含一个属性,该属性保存要在RecyclerView中显示的数据:

The ViewModel is quite simple, it consists just of one property that holds the data to display in the RecyclerView:

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

通常,由于隐式重构支持,我喜欢尽可能多地使用MvvmCross流畅的API.因此,在我的活动中,我像这样绑定MvxRecyclerView的属性:

Generally I like to use the MvvmCross fluent API as much as possible because of the implicit refactoring support.So in my activity, I am binding a property of the MvxRecyclerView like this:

var recyclerView = View.FindViewById<MvxRecyclerView>(Resource.Id.my_recycler_view);
var set = this.CreateBindingSet<MainView, MainViewModel>();
set.Bind(recyclerView)
    .For(v => v.ItemsSource)
    .To(vm => vm.ViewModelItems);
set.Apply();

到目前为止,一切都很好.现在,项目模板的布局文件基本上只包含一个TextView:

So far so good. Now, the layout file for the item template basically just contains a TextView:

<LinearLayout>
    <TextView
        android:id="@+id/innerText" />
</LinearLayout>

我的ViewModelItem类看起来像这样:

public class ViewModelItem
{
    public string Title { get; set; }
}

我现在的问题是,如何使用流畅的API将TextView.Text属性绑定到ViewModelItem.Title属性?

My question now is, how and where do I bind the TextView.Text property to the ViewModelItem.Title property using the fluent API?

我知道在项目模板布局文件中提供一个MvxBind属性,不用流利的API就能很容易地做到这一点,但是我真的更喜欢流利的API解决方案.

I know it is quite easy to do without the fluent API by supplying an MvxBind attribute in the item template layout file, but I would really prefer a fluent API solution.

推荐答案

从MvxRecyclerAdapter继承,并为RecyclerView创建自定义适配器.覆盖OnCreateViewHolder并返回自定义ViewHolder.

Inherit from MvxRecyclerAdapter and create a custom Adapter for your RecyclerView. Override OnCreateViewHolder and return a custom 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);
    }
}

在此ViewHolder中,您可以使用Fluent API进行绑定.

Within this ViewHolder you can use the Fluent API for binding.

public class MyViewHolder : MvxRecyclerViewHolder
{
    private readonly TextView textView;

    public MyViewHolder(View itemView, IMvxAndroidBindingContext context)
        : base(itemView, context)
    {
        this.textView = itemView.FindViewById<TextView>(Android.Resource.Id.Text1);

        this.DelayBind(() =>
        {
            var set = this.CreateBindingSet<MyViewHolder, ViewModelItem>();
            set.Bind(this.textView).To(x => x.Title);
            set.Apply();
        });
    }
}

在活动"中创建适配器并将其添加到RecyclerView:

In your Activity create the Adapter and add it to your RecyclerView:

var adapter = new MyAdapter((IMvxAndroidBindingContext)this.BindingContext);
recyclerView.Adapter = adapter;

并将您的项目绑定到适配器的ItemsSource:

and bind your Items to the ItemsSource of your Adapter:

set.Bind(this.adapter).For(x => x.ItemsSource).To(x => x.ViewModelItems);

这篇关于如何在Android上使用MvvmCross fluent API将RecyclerView项目的TextView绑定到其ViewModel的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:43