动态更新Android RecyclerView中的嵌套控件

在开发Android应用时,经常需要处理列表视图,而RecyclerView是处理列表数据的首选方式。本文将介绍如何在Xamarin Android中创建RecyclerView,并在每个列表项中添加Spinner控件,以及如何根据用户选择的Spinner下拉值动态更新列表项。

创建ViewHolder类

首先,需要创建一个ViewHolder类,用于引用视图对象。ViewHolder类中包含Spinner对象,并在构造函数中为Spinner对象分配事件监听器。

public class VegeViewHolder : RecyclerView.ViewHolder { public ImageView Image { get; set; } public TextView Name { get; set; } public Spinner Quantity { get; set; } public TextView Price { get; set; } public TextView TotalAmount { get; set; } public VegeViewHolder(View itemView, Action itemlistner, Action spinnerItemSelected) : base(itemView) { Image = itemView.FindViewById(Resource.Id.list_image); Name = itemView.FindViewById(Resource.Id.Name); Price = itemView.FindViewById(Resource.Id.Price); Quantity = itemView.FindViewById(Resource.Id.spinner1); TotalAmount = itemView.FindViewById(Resource.Id.total); itemView.Click += (sender, e) => itemlistner(base.Position); Quantity.ItemSelected += new EventHandler(spinnerItemSelected); } }

创建自定义适配器类

接下来,需要创建一个自定义适配器类。RecyclerView需要提供一个适配器,用于访问数据源并填充每个列表项。在适配器类中,创建了一个事件处理器,用于处理Spinner控件的事件。

public event EventHandler ItemClick; public event EventHandler SpinnerItemSelectionChanged; public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.list_items, parent, false); VegeViewHolder vh = new VegeViewHolder(itemView, OnClick, spinner_ItemSelected); return vh; } void OnClick(int position) { if (ItemClick != null) ItemClick(this, position); } void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e) { if (SpinnerItemSelectionChanged != null) SpinnerItemSelectionChanged(sender, e); }

实现OnBindViewHolder方法

当布局管理器准备在RecyclerView的可见屏幕区域显示特定视图时,它会调用适配器的OnBindViewHolder方法,以从数据源填充指定行位置的项目。

public override void OnBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { var item = Items[position]; var vh = viewHolder as VegeViewHolder; var spinnerPos = 0; var adapter = new ArrayAdapter(Context, Android.Resource.Layout.SimpleSpinnerItem, _quantity); adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem); vh.Name.Text = item.Name; vh.Price.Text = string.Format("Price: ${0}", item.Price); vh.ItemView.Tag = position; if (item.Quantity > 0) { spinnerPos = adapter.GetPosition(item.Quantity.ToString()); vh.TotalAmount.Text = string.Format("${0}", item.Price * item.Quantity); } else { vh.TotalAmount.Text = ""; } vh.Quantity.Tag = position; vh.Quantity.Adapter = adapter; vh.Quantity.SetSelection(spinnerPos); vh.Image.SetImageResource(item.ImageId); }

在MainActivity中处理事件

最后,需要在MainActivity类中处理事件。当用户触摸或更改Spinner值时,这些事件将在活动类级别处理。

void OnItemClick(object sender, int position) { int itemPosition = position + 1; Toast.MakeText(this, "Vegetable Item " + itemPosition, ToastLength.Short).Show(); } void SpinnerItemSelectionChangedEvent(object sender, AdapterView.ItemSelectedEventArgs e) { Spinner spinner = (Spinner)sender; var itemPosition = Convert.ToInt32(spinner.Tag); var currentquantity = vegeList[itemPosition].Quantity; var selectedQuantity = Convert.ToInt32(spinner.GetItemAtPosition(e.Position).ToString()); if (currentquantity != selectedQuantity) { vegeList[itemPosition].Quantity = selectedQuantity; mAdapter.NotifyItemChanged(itemPosition); } }
沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485