MvxRecyclerView Fluent API绑定 [英] MvxRecyclerView Fluent API binding
问题描述
我无法使用Fluent API将MvxRecyclerView(或其适配器)的ItemClick绑定到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.
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!).
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屋!