我在Visual Studio 2017上使用RecyclerView(Xamarin.Android),这是我的第一个Xamarin项目,因为我是一名原生的Android开发人员,而且我对于实施On Items Clicks事件感到困惑。 Recycler View。我刚刚使用IDE提供的默认模板(RecyclerAdapter类)创建了RecyclerView.Adapter,它看起来很像本机实现:
这是我的代码:
using System;
using Android.Views;
using Android.Widget;
using Android.Support.V7.Widget;
namespace Airlink
{
class PdfAdapter : RecyclerView.Adapter
{
public event EventHandler<PdfAdapterClickEventArgs> ItemClick;
public event EventHandler<PdfAdapterClickEventArgs> ItemLongClick;
Pdf[] items;
public PdfAdapter(Pdf[] data)
{
items = data;
}
// Create new views (invoked by the layout manager)
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup
parent, int viewType)
{
//Setup your layout here
View itemView = null;
//var id = Resource.Layout.__YOUR_ITEM_HERE;
itemView=LayoutInflater.From(parent.Context).
Inflate(Resource.Layout.pdf_item, parent, false);
var vh = new PdfAdapterViewHolder(itemView, OnClick,
OnLongClick);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
public override void OnBindViewHolder(RecyclerView.ViewHolder
viewHolder, int position)
{
var item = items[position];
// Replace the contents of the view with that element
PdfAdapterViewHolder holder = viewHolder as
PdfAdapterViewHolder;
holder.pdf_name.Text = items[position].Name;
}
public override int ItemCount => items.Length;
void OnClick(PdfAdapterClickEventArgs args) =>
ItemClick?.Invoke(this, args);
void OnLongClick(PdfAdapterClickEventArgs args) =>
ItemLongClick?.Invoke(this, args);
}
public class PdfAdapterViewHolder : RecyclerView.ViewHolder
{
public TextView pdf_name { get; set; }
public PdfAdapterViewHolder(View itemView,
Action<PdfAdapterClickEventArgs> clickListener,
Action<PdfAdapterClickEventArgs> longClickListener) :
base(itemView)
{
pdf_name = itemView.FindViewById<TextView>
(Resource.Id.pdf_name);
itemView.Click += (sender, e) => clickListener(new
PdfAdapterClickEventArgs { View = itemView, Position =
AdapterPosition });
itemView.LongClick += (sender, e) => longClickListener(new
PdfAdapterClickEventArgs { View = itemView, Position =
AdapterPosition });
}
}
public class PdfAdapterClickEventArgs : EventArgs
{
public View View { get; set; }
public int Position { get; set; }
}
}
所以,鉴于我的代码,我只是想知道在哪里可以处理点击的视图,我不确定是否必须在PdfAdapterClickEventArgs类或接收PdfAdapterClickEventArgs对象的OnClick和OnLongClick空洞上实现一些代码,假设我想显示一个Toast,显示View中单击的TextView的名称。我使用以下方法在原生Android上处理此操作:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//my code here
}
}
在ViewHolder构造函数方法中。
我的意思是,我可以更改默认的C#实现,以获得类似于上面代码段的内容,但我想保留原始的C#代码。
答案 0 :(得分:1)
您将在Activity / Fragment中实现OnClick事件处理程序,其中创建了Adapter的实例。
此行表示您正在适配器类中公开ItemClick
事件。
public event EventHandler<PdfAdapterClickEventArgs> ItemClick;
所以你现在能够做到这一点:
var adapter = new PdfAdapter(data);
adapter.ItemClick += OnItemClick;
....
....
myRecyclerView. SetAdapter(adapter);
你将有一个方法:
public void OnItemClick(object sender, PdfAdapterClickEventArgs e)
{
var view = args.View; //this is your view
Toast.MakeText(this, $"Item Position: {args?.Position}", ToastLength.Short).Show();
}
希望这有帮助。