我想分享我在创建一个recycleView时遇到的这个问题,它有一个删除项目的按钮。以防有人在那里浪费了他整整一天的时间来试图解决这个问题。
每当我点击按钮时,它有时会删除两个项目(调用两次)
这是代码(简化)
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
MyView myHolder = holder as MyView;
int IndexPosition = (Cards.Count - 1) - position;
myHolder.tvTitle.Text = Cards[IndexPosition].Title;
myHolder.tvSubTitle.Text = Cards[IndexPosition].SubTitle;
myHolder.tvTime.Text = Cards[IndexPosition].Time;
myHolder.mMainView.Click += (o, e) =>
{
//delete the item, console.write("something")
};
}
简单地说,"某事"写了两次..
答案 0 :(得分:2)
首先:不要在OnBindViewHolder
INSTEAD 您必须在此方法中设置点击事件:
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
例如
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.CardView_proto,parent,false);
TextView Title = row.FindViewById<TextView>(Resource.Id.cardview_textView_main);
Button buttonCheck = row.FindViewById<Button>(Resource.Id.cardview_button_check);
MyView view = new MyView(row) { tvTitle = Title };
buttonCheck.Click += (o,e) =>
{
**//your method here won't be called twice**
};
return view;
};