Recyclerview ViewHolder 中的 TextView - OnClickListner
TextView inside Recyclerview ViewHolder - OnClickListner
我有一个非常简单的 RecyclerView
和一个 ViewHolder
如下
ViewHolder
private class SENT_VH : RecyclerView.ViewHolder, View.IOnClickListener
{
public SENT_VH(View itemView) : base(itemView)
{
MyText = itemView.FindViewById<TextView>(Resource.Id.sent_msg_time);
DeleteButton = itemView.FindViewById<TextView>(Resource.Id.sent_msg_person_name);
itemView.SetOnClickListener(this);
}
public TextView DeleteButton { get; set; }
public TextView MyText { get; set; }
public void OnClick(View v)
{
// ViewHolder Item clicked
}
}
当我点击该项目时,它工作得很好,但我正在尝试在 DeleteButton
上实现 OnClickListner,如下所示
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
try
{
SENT_VH viewHolder = holder as SENT_VH;
viewHolder.MyText.Text = itemList[position].userText;
viewHolder.DeleteButton.SetOnClickListener(new DeleteOnClickListner(itemList[position].id, context, position));
}
catch (Exception X)
{
//LOG.Error >> ERROR
}
}
最后我的DeleteOnClickListner
是
class DeleteOnClickListner : Java.Lang.Object, View.IOnClickListener
{
private decimal id;
private Context context;
private int pos;
public DeleteOnClickListner(decimal id, Context context, int pos)
{
this.id = id;
this.context = context;
this.pos = pos;
}
public void OnClick(View v)
{
Android.Util.Log.Debug("ADAPTER", "Delete id: " + id);
((MainActivity)context).deleteConfirmation(id, pos);
}
}
在 deleteConfirmation
方法的 MainActivity 中,我只是试图获取以下位置和 ID
public void deleteConfirmation(decimal id, int pos)
{
EVENT_LOGGER(TAG, "Pos:" + pos + ", id: " + id);
}
问题是我总是得到与第一个 id
和 position
相同的 DeleteButton
我点击任何 ViewHolder,它永远不会改变。
例如,如果我第一次点击项目位置 10 的 DeleteButton
,无论我接下来点击哪个 DeleteButton
,它都会继续发送位置 10。
你们知道发生了什么吗
EDIT:
按照@tyczj 的建议,我尝试通过在 ViewHolder 中添加以下内容来移动 ViewHolder 中的 OnclickListner
public void SetMyView(decimal id, Context context, int pos)
{
DeleteButton.SetOnClickListener(new DeleteOnClickListner(id, context, pos));
}
并在我的 OnBindViewHolder
中使用 SetMyView,如下所示
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
try
{
SENT_VH viewHolder = holder as SENT_VH;
viewHolder.SetMyView(position, context); //This line here
viewHolder.MyText.Text = itemList[position].userText;
viewHolder.DeleteButton.SetOnClickListener(new DeleteOnClickListner(itemList[position].id, context, position));
}
没有差异完全相同的结果
赶上(异常 X)
{
//LOG.Error >> 错误
}
}
EDIT 2 - Working Solution
我更新了 VH 以添加以下内容
DeleteButton.SetOnClickListener(new DeleteOnClickListner(AdapterPosition));
您可以在 ViewHolder
中使用 getAbsoluteAdapterPosition
来获取绝对位置
你的第一种方法有什么问题?使用 ViewHolder 比你正在做的要容易得多
使您的取景器成为适配器的内部 class
Kotlin 示例
inner class ViewHolder(view: View): RecyclerView.ViewHolder(view){
val MyText : TextView = view.findViewById(R.id.sent_msg_time)
val DeleteButton : TextView = view.findViewById(R.id.sent_msg_person_name)
init {
DeleteButton .setOnClickListener {
((MainActivity)context).deleteConfirmation(itemList[adapterPosition].id, adapterPosition)
}
}
}
adapterPosition
或 absoluteAdapterPosition
取决于您使用的 recyclerview 版本为您提供查看器的位置
为您的适配器传递上下文以获取您的主要 activity(虽然应该使用一个接口来回传给 activity)和项目列表,然后您可以获得您需要的东西容易多了
我有一个非常简单的 RecyclerView
和一个 ViewHolder
如下
ViewHolder
private class SENT_VH : RecyclerView.ViewHolder, View.IOnClickListener
{
public SENT_VH(View itemView) : base(itemView)
{
MyText = itemView.FindViewById<TextView>(Resource.Id.sent_msg_time);
DeleteButton = itemView.FindViewById<TextView>(Resource.Id.sent_msg_person_name);
itemView.SetOnClickListener(this);
}
public TextView DeleteButton { get; set; }
public TextView MyText { get; set; }
public void OnClick(View v)
{
// ViewHolder Item clicked
}
}
当我点击该项目时,它工作得很好,但我正在尝试在 DeleteButton
上实现 OnClickListner,如下所示
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
try
{
SENT_VH viewHolder = holder as SENT_VH;
viewHolder.MyText.Text = itemList[position].userText;
viewHolder.DeleteButton.SetOnClickListener(new DeleteOnClickListner(itemList[position].id, context, position));
}
catch (Exception X)
{
//LOG.Error >> ERROR
}
}
最后我的DeleteOnClickListner
是
class DeleteOnClickListner : Java.Lang.Object, View.IOnClickListener
{
private decimal id;
private Context context;
private int pos;
public DeleteOnClickListner(decimal id, Context context, int pos)
{
this.id = id;
this.context = context;
this.pos = pos;
}
public void OnClick(View v)
{
Android.Util.Log.Debug("ADAPTER", "Delete id: " + id);
((MainActivity)context).deleteConfirmation(id, pos);
}
}
在 deleteConfirmation
方法的 MainActivity 中,我只是试图获取以下位置和 ID
public void deleteConfirmation(decimal id, int pos)
{
EVENT_LOGGER(TAG, "Pos:" + pos + ", id: " + id);
}
问题是我总是得到与第一个 id
和 position
相同的 DeleteButton
我点击任何 ViewHolder,它永远不会改变。
例如,如果我第一次点击项目位置 10 的 DeleteButton
,无论我接下来点击哪个 DeleteButton
,它都会继续发送位置 10。
你们知道发生了什么吗
EDIT:
按照@tyczj 的建议,我尝试通过在 ViewHolder 中添加以下内容来移动 ViewHolder 中的 OnclickListner
public void SetMyView(decimal id, Context context, int pos)
{
DeleteButton.SetOnClickListener(new DeleteOnClickListner(id, context, pos));
}
并在我的 OnBindViewHolder
中使用 SetMyView,如下所示
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
try
{
SENT_VH viewHolder = holder as SENT_VH;
viewHolder.SetMyView(position, context); //This line here
viewHolder.MyText.Text = itemList[position].userText;
viewHolder.DeleteButton.SetOnClickListener(new DeleteOnClickListner(itemList[position].id, context, position));
}
没有差异完全相同的结果 赶上(异常 X) { //LOG.Error >> 错误 } }
EDIT 2 - Working Solution
我更新了 VH 以添加以下内容
DeleteButton.SetOnClickListener(new DeleteOnClickListner(AdapterPosition));
您可以在 ViewHolder
中使用 getAbsoluteAdapterPosition
来获取绝对位置
你的第一种方法有什么问题?使用 ViewHolder 比你正在做的要容易得多
使您的取景器成为适配器的内部 class
Kotlin 示例
inner class ViewHolder(view: View): RecyclerView.ViewHolder(view){
val MyText : TextView = view.findViewById(R.id.sent_msg_time)
val DeleteButton : TextView = view.findViewById(R.id.sent_msg_person_name)
init {
DeleteButton .setOnClickListener {
((MainActivity)context).deleteConfirmation(itemList[adapterPosition].id, adapterPosition)
}
}
}
adapterPosition
或 absoluteAdapterPosition
取决于您使用的 recyclerview 版本为您提供查看器的位置
为您的适配器传递上下文以获取您的主要 activity(虽然应该使用一个接口来回传给 activity)和项目列表,然后您可以获得您需要的东西容易多了