我正在尝试使用ReactiveRecyclerViewAdapter基于ViewModel上的SourceList更新RecyclerView,但是有关此问题的文档并不多,但是我不应该是唯一为此苦苦挣扎的人。
ViewModel:
public class PassportScreenViewModel : ReactiveObject
{
readonly IAccountService _accountService;
public PassportScreenViewModel(IAccountService accountService = null)
{
_accountService = accountService ?? Locator.Current.GetService<IAccountService>();
SchoolChoices = new SourceList<SchoolDataModel>();
SchoolChoices.Add(new SchoolDataModel(new SchoolChoice() { Name = "My simple test" }));
SchoolChoices.Add(new SchoolDataModel(new SchoolChoice() { Name = "My simple tes 2" }));
AddSchoolCommand = ReactiveCommand.Create(() => {
SchoolChoices.Edit(x => x.Add(new SchoolDataModel(new SchoolChoice() { Name = "My simple test " + SchoolChoices.Count })));
});
}
[Reactive] public string Headline { get; set; }
[Reactive] public string Title { get; set; }
[Reactive] public string Company { get; set; }
[Reactive] public SourceList<SchoolDataModel> SchoolChoices { get; set; }
public ReactiveCommand AddSchoolCommand { get; set; }
}
我的片段:
[WireUpResource("recyclerView_passport_schools")]
public RecyclerView SchoolsRecycler { get; private set; }
[WireUpResource("textview_passport_addschool")]
public Button AddSchoolButton { get; private set; }
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.fragment_passportscreen, container, false);
this.WireUpControls(view);
this.ViewModel = new PassportScreenViewModel();
Photo.Click += Photo_Click;
this.BindCommand(ViewModel, viewModel => viewModel.AddSchoolCommand, myView => myView.AddSchoolButton);
var adapter = new SchoolReactiveAdapter(ViewModel.SchoolChoices.Connect());
SchoolsRecycler.HasFixedSize = true;
SchoolsRecycler.SetLayoutManager(new LinearLayoutManager(Activity));
SchoolsRecycler.SetAdapter(adapter);
ViewModel.SchoolChoices.Connect().Subscribe(x =>
{
//adapter = new SchoolReactiveAdapter(ViewModel.SchoolChoices.Connect());
//SchoolsRecycler.SwapAdapter(adapter, true);
});
return view;
}
我的适配器:
public class SchoolReactiveAdapter : ReactiveRecyclerViewAdapter<SchoolDataModel>
{
public SchoolReactiveAdapter(IObservable<IChangeSet<SchoolDataModel>> backingList) : base(backingList)
{
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
var itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.item_school, parent, false);
return new MySchoolViewHolder(itemView);
}
public class MySchoolViewHolder : ReactiveRecyclerViewViewHolder<SchoolDataModel>
{
[WireUpResource("textview_schoolitem_name")]
public TextView SchoolName { get; private set; }
public MySchoolViewHolder(View view) : base(view)
{
this.WireUpControls();
this.Bind(ViewModel, vm => vm.Name, v => v.SchoolName.Text);
}
}
}
如果我对此没有评论:
ViewModel.SchoolChoices.Connect().Subscribe(x =>
{
//adapter = new SchoolReactiveAdapter(ViewModel.SchoolChoices.Connect());
//SchoolsRecycler.SwapAdapter(adapter, true);
});
这将交换适配器,并且新项目将显示,但是我认为当我执行Connect()时,适配器应该对ViewModel的ChangeSet做出反应;
var adapter = new SchoolReactiveAdapter(ViewModel.SchoolChoices.Connect());
即使List获得的是一个新商品,RecyclerView也不会得到更新。没有关于如何使用ReactiveUI完全设置绑定的RecyclerView的文档,至少我找不到它,你们中的任何一个都可以帮助我吗?
谢谢