与xamarin android应用程序有关的问题是,一旦我在适配器中的任何项目上调用click事件它将工作但它调用的不仅仅是我点击的那个,意思是例如我点击共享和共享意图将显示但不仅仅一次列表视图适配器中的多个项目。这是适配器中的代码。请告诉我如何解决这个问题。我已尝试过代理人,甚至在点击
上禁用其他listview项目代码:
namespace NameSpace
{
public class dashboardAdapter : BaseAdapter<DashboardLINQ>
{
List<DashboardLINQ> items;
Activity context;
string mid;
public dashboardAdapter(Activity context, List<DashboardLINQ> items)
: base()
{
this.context = context;
this.items = items;
}
public override long GetItemId(int position)
{
return position;
}
public override DashboardLINQ this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
{
view = LayoutInflater.From(context).Inflate (Resource.Layout.dashboardItemLayout, null, false);
}
ImageView prof = view.FindViewById<ImageView>(Resource.Id.imageView1);
Koush.UrlImageViewHelper.SetUrlDrawable (prof, "http://www.mymentor.org.za/"+items [position].uPicLocal);
mid = items [position].MID;
TextView usrname = view.FindViewById<TextView> (Resource.Id.textView1);
usrname.Text = items [position].uFullName;
TextView date = view.FindViewById<TextView> (Resource.Id.textView2);
date.Text = items [position].pDate;
ImageView imagen = view.FindViewById<ImageView>(Resource.Id.imageView2);
Koush.UrlImageViewHelper.SetUrlDrawable (imagen, "http://www.mymentor.org.za/"+items [position].pImage);
TextView desc = view.FindViewById<TextView> (Resource.Id.textView3);
desc.Text = Html.FromHtml (items [position].pDesc).ToString();
TextView comlike = view.FindViewById<TextView> (Resource.Id.textView4);
comlike.Text = "Likes: "+items [position].likecnumrows+" Comments: "+items [position].commentscnumrows;
ImageButton like = view.FindViewById<ImageButton> (Resource.Id.button1);
ImageButton comment = view.FindViewById<ImageButton> (Resource.Id.button2);
ImageButton share = view.FindViewById<ImageButton> (Resource.Id.button3);
like.Click += delegate(object sender, EventArgs e) {
WebClient client = new WebClient();
Uri uri = new Uri("url");
NameValueCollection parameters = new NameValueCollection();
parameters.Add("UID", items[position].MID);
parameters.Add("PID", items[position].ID);
client.UploadValuesAsync(uri, parameters);
like.SetImageResource(Resource.Drawable.ButtonComment);
};
share.Focusable = false;
share.FocusableInTouchMode = false;
share.Clickable = true;
share.Click += delegate(object sender, EventArgs e) {
int MaxLength = 150;
var name = Html.FromHtml (items [position].pDesc).ToString();
if (name.Length > MaxLength) {
name = name.Substring(0, MaxLength) + "... My Mentor";
}
var myIntent = new Intent(Android.Content.Intent.ActionSend);
myIntent.PutExtra(Intent.ExtraText, name);
myIntent.SetType("text/plain");
this.context.StartActivity(Intent.CreateChooser(myIntent,"Choose an App"));
};
return view;
}
}
答案 0 :(得分:2)
这种情况发生了,因为Android重复使用行并调用GetView方法多次次(例如,当您滚动列表视图时,Android会自动启动GetView以绘制隐藏项目以及为什么您的“点击次数”会重复) 。
因此,您可以尝试使用 ViewHolder Pattern 。
看看这个(我的小例子):
MainLayout.axml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lst" />
</LinearLayout>
和ListItem.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/relativeLayout1">
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt"
android:layout_centerInParent="true" />
<Button
android:text="PUSH ME"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Push"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
OnCreate()方法
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
ListView lst = FindViewById<ListView>(Resource.Id.lst);
List<string> Data = new List<string>();
Data.Add("Prosto GG");
Data.Add("Prosto GG");
Data.Add("Prosto GG");
Data.Add("Prosto GG");
Data.Add("Prosto GG");
Data.Add("Prosto GG");
Data.Add("Prosto GG");
Data.Add("Prosto GG");
Data.Add("Prosto GG");
Data.Add("Prosto GG");
Data.Add("Prosto GG");
Data.Add("Prosto GG");
Data.Add("Prosto GG");
Data.Add("Prosto GG");
Data.Add("Prosto GG");
Data.Add("Prosto GG");
CustomAdapter cst = new CustomAdapter(Data, this);
lst.Adapter = cst;
}
}
这篇文章的主要部分是 CustomAdapter 类:
class CustomAdapter: BaseAdapter<string>
{
Dialog alertDialog;
int count = 0;
List<string> _Items;
Context _context;
public CustomAdapter(List<string> Items,Context context)
{
this._Items = Items;
this._context = context;
}
#region implemented abstract members of BaseAdapter
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
ServiceViewHolder holder = null;
var view = convertView;
if (view == null)
{
////this is part of ViewHolder pattern,new istance of ViewHolder
holder = new ServiceViewHolder();
view = LayoutInflater.From(_context).Inflate(Resource.Layout.ListItem, null);
holder.Txt = view.FindViewById<TextView>(Resource.Id.txt);
holder.PushME = view.FindViewById<Button>(Resource.Id.Push);
//put your handler here,in this block of code,like this
holder.PushME.Click += (sender, e) =>
{
count++;
var builder = new AlertDialog.Builder(_context);
builder.SetTitle("test");
builder.SetMessage(holder.Txt.Text + " " + count);
builder.SetCancelable(false);
builder.SetPositiveButton("OK", new EventHandler<DialogClickEventArgs>((sender1, e2) =>
{
alertDialog.Dismiss();
}));
alertDialog = builder.Create();
alertDialog.SetCanceledOnTouchOutside(false);
alertDialog.Show();
};
view.Tag = holder;
}
else
{
holder = view.Tag as ServiceViewHolder;
}
holder.Txt.Text = _Items[position];
return view;
}
public override int Count
{
get
{
return _Items.Count;
}
}
#endregion
#region implemented abstract members of BaseAdapter
public override string this[int index]
{
get
{
return _Items[index];
}
}
#endregion
}
public class ServiceViewHolder : Java.Lang.Object
{
public Button PushME { get; set; }
public TextView Txt { get; set; }
}
因此,如果你想使用这种模式,你必须这样做:
您的案例如下:
if(view == null)
{
//inflate layout and other stuff.
holder.like.Click += delegate(object sender, EventArgs e) {
WebClient client = new WebClient();
Uri uri = new Uri("url");
NameValueCollection parameters = new NameValueCollection();
parameters.Add("UID", items[position].MID);
parameters.Add("PID", items[position].ID);
client.UploadValuesAsync(uri, parameters);
holder.like.SetImageResource(Resource.Drawable.ButtonComment);
};
//etc
}
有关详细信息,请阅读此article!
享受。