自定义Listview适配器ImageView单击

时间:2017-09-04 11:37:07

标签: android listview xamarin xamarin.android

目前我已经实现了一个自定义列表视图,它有一个适配器,在listview行中是一个我可以点击的ImageView:

        public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var item = _items[position];
        var view = convertView;
        ListViewHolder listViewHolder;
        if (view == null)
        {
            listViewHolder = new ListViewHolder();
            var inflater = LayoutInflater.FromContext(_context);
            view = inflater.Inflate(Resource.Layout.foorow, parent, false);
            listViewHolder.cImage = view.FindViewById<ImageView>(Resource.Id.cImg);        
            listViewHolder.cText = view.FindViewById<TextView>(Resource.Id.cTxt);
            listViewHolder.cSet = view.FindViewById<TextView>(Resource.Id.cSet);
            listViewHolder.cType = view.FindViewById<TextView>(Resource.Id.cType);
            listViewHolder.cId = view.FindViewById<TextView>(Resource.Id.cId);
            listViewHolder.cImage.Click += (object sender, EventArgs e) =>
            {
                _listFragment.ShowLargeImage(item);
            };
            view.Tag = listViewHolder;
        }
        else
        {
            listViewHolder = (ListViewHolder)view.Tag;
        }

        if (item != null)
        {
            try
            {
                Glide.With(_context)
                    .Load(item.ImageUrl)
                    .DiskCacheStrategy(DiskCacheStrategy.All)
                    //todo .Placeholder()
                    .Into(listViewHolder.cImage);
                listViewHolder.cText.Text = item.Name;
                listViewHolder.cSet.Text = item.Set;
                listViewHolder.cId.Text = item.Id;
                listViewHolder.cType.Text = string.Join(",", item.Types.Select(i => i));
                if (!listViewHolder.cImage.HasOnClickListeners)
                {
                    listViewHolder.cImage.Click += (sender, e) =>
                    {
                        _listFragment.ShowLargeImage(item);
                    };
                }
            }
            catch (Exception ex)
            {
                var xx = ex;
            }

        }

        return view;
    }

我遇到的问题是,当单击其中一个ImageView时,它实际上会返回错误的项目,从而显示错误的图像。

我猜这与listview在内存和定位中保存的方式有关但我无法解决这个问题。

我尝试过设置imageview点击代码并重复使用,但这似乎没什么帮助。

0 个答案:

没有答案