Dynamic GridVIew ItemClick在xamarin android中不工作

本文关键字:工作 android xamarin GridVIew ItemClick Dynamic | 更新日期: 2023-09-27 18:25:40

我创建了一个动态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在我不使用自定义适配器类的情况下工作。

做什么…任何想法或参考。请帮忙。

Dynamic GridVIew ItemClick在xamarin android中不工作

尝试此操作,您需要注册点击事件

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;
        }