我创建了一个动态GridView,它有一个自定义Adapterclass。现在我尝试在GridView上获取点击的位置和位置名称。 为此,我使用了ItemClick,问题是它不起作用。
这是我的代码。
这是我的自定义适配器
using System;
using Android.Widget;
using Android.App;
using System.Collections.Generic;
using Android.Views;
using Android.Graphics;
using System.Net;
using Square.Picasso;
using Android.Util;
namespace AMUSEAndroid
{
public class GridViewAdapter : BaseAdapter<ImageItem>
{
Activity context;
List<ImageItem> items;
public GridViewAdapter (Activity context, List<ImageItem> gridViewtems)
{
this.context = context;
this.items = gridViewtems;
}
public override int Count {
get { return items.Count; }
}
public override ImageItem this[int position]
{
get { return items[position]; }
}
public override long GetItemId (int position)
{
return position;
}
public override View GetView (int position, View convertView, ViewGroup parent)
{
View view = convertView;
var item = items[position];
if (view == null)
view = context.LayoutInflater.Inflate(Resource.Layout.player_grid_item, null);
ImageView imgIcon = view.FindViewById<ImageView> (Resource.Id.img_play_bg);//.SetImageBitmap (gridViewtems[position]);
view.FindViewById<TextView> (Resource.Id.music_text).Text = item.Heading;
/*imgIcon.Click += delegate(object sender, EventArgs e) {
Toast.MakeText (context, "Click"+position, ToastLength.Short).Show ();
Log.Info("BIKAHS","click at"+position);
};*/
/*var imageBitmap = GetImageBitmapFromUrl(item.ImageResourceId);
imgIcon.SetImageBitmap(imageBitmap);*/
Picasso.With(context)
.Load(item.ImageResourceId)
//.Placeholder(Resource.Drawable.place_holder)
.Into(imgIcon);
return view;
}
void ImgIcon_Click (object sender, EventArgs e)
{
//Toast.MakeText (context, "Click"+position, ToastLength.Short).Show ();
}
}
}
当我不使用自定义适配器时,ItemCLick正在工作。
怎么做..任何想法或参考。请帮忙。
答案 0 :(得分:1)
试试这个你需要注册点击事件
public override View GetView (int position, View convertView, ViewGroup parent)
{
View view = convertView;
var item = items[position];
if (view == null)
view = context.LayoutInflater.Inflate(Resource.Layout.player_grid_item, null);
ImageView imgIcon = view.FindViewById<ImageView> (Resource.Id.img_play_bg);//.SetImageBitmap (gridViewtems[position]);
view.FindViewById<TextView> (Resource.Id.music_text).Text = item.Heading;
//register the click event
imgIcon.Click+=ImgIcon_Click
Picasso.With(context)
.Load(item.ImageResourceId)
//.Placeholder(Resource.Drawable.place_holder)
.Into(imgIcon);
return view;
}
void ImgIcon_Click (object sender, EventArgs e)
{
var name=items[e.postion].heading;
}