[英]Why is my RecyclerView not responding to my custom Click-Event?
I am using the this tutorial.我正在使用本教程。
I am using API 27 and Xamarin Android without Forms.我正在使用没有表单的 API 27 和 Xamarin Android。
The basic usage of a RecyclerView
is explained in the previous step of this tutorial and the next step is to extend the sample by Click-Handlers. RecyclerView
的基本用法在本教程的上一步中进行了解释,下一步是通过 Click-Handlers 扩展示例。
I have extended my Adapter with this code public event EventHandler<int> ItemClick;
我用这个代码public event EventHandler<int> ItemClick;
扩展了我的适配器public event EventHandler<int> ItemClick;
and created a callback function like this:并创建了一个这样的回调函数:
void OnItemClick (object sender, int position)
{
Console.WriteLine("I've done it!");
}
and added it to the Adapter like this:并将其添加到适配器中,如下所示:
mAdapter = new CustomObjectAdapter(mCustomObject);
mAdapter.ItemClick += OnItemClick;
Once the app shows up I have set a Breckpoint on the Console.WriteLine
-Part and touched/clicked the shown image.一旦应用程序出现,我就在Console.WriteLine
-Part 上设置了一个 Breckpoint 并触摸/单击显示的图像。 But the event doesn't fire.但事件不会触发。
This is my adapter-class:这是我的适配器类:
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;
}
}
And this is how I set-up my adapter:这就是我设置适配器的方式:
private void SetupView()
{
RunOnUiThread(() =>
{
mModuleClassContainerAdapter = new ModuleClassContainerAdapter(mModuleClassContainer);
mModuleClassContainerAdapter.ItemClick += MModuleClassContainerAdapter_ItemClick;
mRecyclerView.SetAdapter(mModuleClassContainerAdapter);
mModuleClassContainerAdapter.NotifyDataSetChanged();
});
}
Why gets my event not fired?为什么我的事件没有被触发?
In your adapter, declare the eventHandler, and pass it to the ViewHolder在您的适配器中,声明 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);
}
In your ViewHolder在你的 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.