我正在使用this tutorial.
我使用的是没有表单的API 27和Xamarin Android。
RecyclerView
的基本用法已在本教程的上一步中进行了说明,下一步是通过Click-Handlers扩展示例。
我已使用以下代码public event EventHandler<int> ItemClick;
扩展了我的适配器,并创建了这样的回调函数:
void OnItemClick (object sender, int position)
{
Console.WriteLine("I've done it!");
}
并将其添加到适配器中,如下所示:
mAdapter = new CustomObjectAdapter(mCustomObject);
mAdapter.ItemClick += OnItemClick;
应用显示后,我在Console.WriteLine
-Part上设置了一个Breckpoint,并触摸/单击了所显示的图像。但是该事件不会触发。
这是我的适配器类:
class ModuleClassContainerAdapter : RecyclerView.Adapter
{
public event EventHandler<int> ItemClick;
public ModuleClassContainer mModuleClassContainer;
public ModuleClassContainerAdapter(ModuleClassContainer moduleClassContainer)
{
mModuleClassContainer = moduleClassContainer;
}
public override int ItemCount
{
get { return mModuleClassContainer.NumModules; }
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
ModuleViewHolder vh = holder as ModuleViewHolder;
var imageBitmap = BitmapFactory.DecodeByteArray(mModuleClassContainer[position].ImageBytes, 0, mModuleClassContainer[position].ImageBytes.Length);
// Load the photo image resource from the photo album:
vh.Image.SetImageBitmap(imageBitmap);
// Load the photo caption from the photo album:
vh.Caption.Text = mModuleClassContainer[position].ModuleName;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
// Inflate the CardView for the photo:
View itemView = LayoutInflater.From(parent.Context).
Inflate(Resource.Layout.ModuleCardView, parent, false);
// Create a ViewHolder to hold view references inside the CardView:
ModuleViewHolder vh = new ModuleViewHolder(itemView);
return vh;
}
}
这是我设置适配器的方式:
private void SetupView()
{
RunOnUiThread(() =>
{
mModuleClassContainerAdapter = new ModuleClassContainerAdapter(mModuleClassContainer);
mModuleClassContainerAdapter.ItemClick += MModuleClassContainerAdapter_ItemClick;
mRecyclerView.SetAdapter(mModuleClassContainerAdapter);
mModuleClassContainerAdapter.NotifyDataSetChanged();
});
}
为什么我的活动没有被触发?
答案 0 :(得分:1)
在适配器中,声明eventHandler,并将其传递给ViewHolder
public class ModuleClassContainerAdapter : RecyclerView.Adapter
{
// Event handler for item clicks:
public event EventHandler<int> ItemClick;
// Create a new photo CardView (invoked by the layout manager):
public override RecyclerView.ViewHolder
OnCreateViewHolder (ViewGroup parent, int viewType)
{
// You need to pass the event
ModuleViewHolder vh = new ModuleViewHolder (itemView, OnClick);
return vh;
}
// Raise an event when the item-click takes place:
void OnClick (int position)
{
if (ItemClick != null)
ItemClick (this, position);
}
在您的ViewHolder中
public class ModuleViewHolder : RecyclerView.ViewHolder
{
// Get references to the views defined in the CardView layout.
public ModuleViewHolder (View itemView, Action<int> listener)
: base (itemView)
{
on the item view and report which item
// was clicked (by layout position) to the listener:
itemView.Click += (sender, e) => listener (base.LayoutPosition);
}
}