我有一个带有一定数量图片的GridView。单击图片时,将启动一个新活动,将其全屏显示。现在发生的奇怪事情是:
当您单击第一张图片(索引0)时,它不会按预期启动活动。对于我的其余照片,它做得很好。但是当我点击第一个项目然后我点击另一个项目时,第一个项目弹出(索引0),其下方有正确的点击项目。当出现这种奇怪的行为时,当我点击关闭按钮时,第一个项目(索引0)重启自己很多次也是非常烦人的。 现在我的适配器代码如下:
class GridAdapter : BaseAdapter
{
private Context c;
private List<string[]> griditems;
private int ColumnWidth;
private LayoutInflater inflater;
private App app;
public GridAdapter(Context c, List<string[]> griditems, int ColumnWidth) {
this.c = c;
this.griditems = griditems;
this.ColumnWidth = ColumnWidth;
inflater = LayoutInflater.From (c);
app = (App)Application.Context;
}
#region implemented abstract members of BaseAdapter
public override Java.Lang.Object GetItem (int position)
{
throw new NotImplementedException ();
}
public override long GetItemId (int position)
{
return position;
}
public override View GetView (int position, View convertView, ViewGroup parent)
{
ImageView ImageView;
if (convertView == null) {
convertView = inflater.Inflate (Resource.Layout.PhotoGridItem, parent, false);
ImageView = (ImageView)convertView.FindViewById<ImageView> (Resource.Id.Photo);
} else {
ImageView = (ImageView)convertView.FindViewById<ImageView> (Resource.Id.Photo);
}
ImageView.SetScaleType (ImageView.ScaleType.CenterCrop);
convertView.LayoutParameters = new GridView.LayoutParams (ColumnWidth, ColumnWidth);
ImageView.LayoutParameters = new RelativeLayout.LayoutParams (ColumnWidth , ColumnWidth);
convertView.Click += (object sender, EventArgs e) => {
var intent = new Intent (Application.Context, typeof(PhotoActivity));
intent.SetFlags(ActivityFlags.NewTask);
intent.PutExtra("position", position);
Application.Context.StartActivity (intent);
};
UrlImageViewHelper.UrlImageViewHelper.SetUrlDrawable (ImageView, griditems.ElementAt(position)[1], Resource.Drawable.no_location);
return convertView;
}
public override int Count {
get {
return this.griditems.Count;
}
}
#endregion
public void Append(List<string[]> items) {
this.griditems.Concat (items);
}
public void Prepend(List<string[]> items) {
items.Reverse ();
items.Concat (this.griditems);
}
public void Replace(List<string[]> items) {
this.griditems.Clear ();
items.Reverse();
this.griditems = items;
}
它开始的活动如下:
public class PhotoActivity : Activity
{
private ViewPager ViewPager;
private PhotoAdapter Adapter;
private App app;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
app = (App)Application.Context;
SetContentView (Resource.Layout.PhotoActivity);
ViewPager = (ViewPager)FindViewById<ViewPager> (Resource.Id.pager);
Intent i = Intent;
int position = i.GetIntExtra ("position", 0);
Adapter = new PhotoAdapter (this, app.PhotoList);
ViewPager.Adapter = Adapter;
ViewPager.CurrentItem = position;
}
protected override void OnDestroy ()
{
ViewPager.Dispose ();
Adapter.Dispose ();
base.OnDestroy ();
Console.WriteLine ("OnDestroy () called");
}
}
有人知道这里发生了什么吗?
答案 0 :(得分:0)
我强烈建议从适配器中取出Click事件。使用GridViews或ListViews时,您希望在GridView上使用ItemClick事件或覆盖自定义ListActivity的OnListItemClick方法。
使用ListActivity时,您可以使用底部的代码,根据您的需要进行调整。
我的合作伙伴有一篇关于如何将Context菜单集成到ListActivity中的博客文章。你可能想查看那篇文章。通常情况下,用例会以单个操作开始,包括单击某个项目,但可能最终您可能希望为任何单击设置一个选项菜单。这是一个链接。
如果要在开始新活动后关闭ListActivity或包含GridView的Activity,只需将this.Finish()添加到下面任一代码示例的底部。
哦,嗯......欢迎来到Xamarin。
gridview.ItemClick += delegate (object sender, ItemEventArgs args) {
var intent = new Intent (Application.Context, typeof(PhotoActivity));
intent.SetFlags(ActivityFlags.NewTask);
intent.PutExtra("position", args.Position);
this.StartActivity (intent);
};
protected override void OnListItemClick(ListView l, View v, int position, long id)
{
var intent = new Intent (Application.Context,typeof(PhotoActivity));
intent.SetFlags(ActivityFlags.NewTask);
intent.PutExtra("position", position);
this.StartActivity (intent);
}