多次点击自定义listview adpater xamarin android中的事件 [英] Multiple times click event fire in custom listview adpater xamarin android

查看:97
本文介绍了多次点击自定义listview adpater xamarin android中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了listview和自定义列表视图适配器,我为自定义适配器的imageview控件生成了一个click事件。现在问题是我的点击事件多次以不同的位置触发。

这是我到目前为止尝试过的一些代码: -



I had used listview with custom listview adapter and i had generated a click event for imageview control of custom adapter. Now the issue is my click event fires multiple time with different position.
Here is some code which i had tried so far :-

<pre lang="C#">
public override View GetView(int position, View convertView, ViewGroup parent)
		{
			try {
				ProductViewHolder holder = null;
				RetriveState();

				if(convertView == null)
				{
					holder = new ProductViewHolder();
					convertView = context.LayoutInflater.Inflate(Resource.Layout.ProductListTabView,parent,false);
					holder.ivPlus = convertView.FindViewById<ImageView> (Resource.Id.ivPlus); 
					convertView.Tag = holder;
				}
				else 
					holder = (ProductViewHolder)convertView.Tag;

				var item = itemsList.ElementAt(position);
			  
				//this is where i am facing the issue coz it fires multiple times
				holder.ivPlus.Click +=  delegate  {
					ProgressDialog progressDialog = ProgressDialog.Show (context, "", "Loading Data.."); 
					int currentQty = item.Quantity; //Convert.ToInt32(holder.tvQty.Text);
					holder.tvQty.Text = (currentQty + 1).ToString(); 
					itemsList.ElementAt(position).Quantity = Convert.ToInt32(holder.tvQty.Text);
					progressDialog.Dismiss(); 
				};
			}
		}





谁能告诉我这里我做错了什么?



Can anyone tell me what i am doing wrong over here?

推荐答案

你好,



我有类似的问题并使用ViewHolder模式修复它。你似乎已经在使用它,所以我想告诉你如何初始化它。

Hello,

I had a similar issue and fixed it by using the ViewHolder-pattern. You seem to already be using it so I would like to show you how I would initialize it.
public override View GetView(int position, View convertView, ViewGroup parent)
{
    ProductViewHolder holder = null;
    RetrieveState();

    if (convertView == null)
        holder = view.Tag as ProductViewHolder;
    if (holder == null)
    {
        holder = new ProductViewHolder();
        convertView = context.LayoutInflater.Inflate(
                Resource.Layout.ProductListTabView,parent,false);
        ...
        holder.Tag = holder;
    }
}



这样即使 convertView 以后要更改为 null 。如果您熟悉该模式,持有者的工作方式类似于Singleton,因为它保证只创建一次。因此声明


This way the holder is not re-initialized multiple times even if convertView were to change to null at a later time. The holder works kind of like a Singleton if you are familiar with that pattern, since it is guaranteed to only be created once. And therefore the statement

holder.ivPlus.Click += delegate {... }



只会被调用一次。



在C#中,你通过使用函数指针(委托)添加一个函数指针+ =运算符。在您的情况下,每次 convertView null 时,都会向处理程序添加一个新的函数指针,从而导致它看起来好像发生了多次点击事件。

没有办法只为事件处理程序分配一个函数指针,它就像一个列表,你可以用 +添加= 或删除 - =


will only get called once.

In C# you add a function pointer (delegate) to an event handler by using the += operator. In your case, you add a new function pointer to the handler to call every time convertView is null, leading to it seeming like multiple click events were fired.
There is no way to assign only one function pointer to an event handler, it works like a list to which you can either add with += or remove with -=.


这篇关于多次点击自定义listview adpater xamarin android中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆