有关ART的CustomAdapter问题的Xamarin Android Gridview

时间:2015-12-02 10:21:57

标签: java c# android xamarin xamarin.android

我在android上遇到gridview和customadapter的问题。 (项目单元格包含图像视图,3个文本视图和2个按钮)

2个物理设备(1个带有android 4.3(dalvik),1个带有android 5.1.1(艺术))。

在dalvik设备上,gridview滚动非常流畅。 在ART设备上,gridview滚动非常慢。

图像由getview中调用的asynctask加载。

有什么建议吗?

以下是我的GetView代码段:

 public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var item = _orderItemLoader.OrderItems[position];

        View itemView = LayoutInflater.From(_context).Inflate(Resource.Layout.OrderItemCell, parent, false);

        var tvDisplayName = itemView.FindViewById<TextView>(Resource.Id.tvDisplayName);
        var tvDisplayAmount = itemView.FindViewById<TextView>(Resource.Id.tvDisplayAmount);
        var tvDisplayCount = itemView.FindViewById<TextView>(Resource.Id.textViewQuantity);

        foreach (var _item in _orderItemLoader.OrderItems)
        {
            if (GetItemId(position) == _item.ItemId)
            {
                tvDisplayCount.Text = _item.Count.ToString();
                break;
            }
        }

        var btnPlus = itemView.FindViewById<Button>(Resource.Id.buttonPlus);
        btnPlus.Focusable = false;
        btnPlus.FocusableInTouchMode = false;
        btnPlus.Clickable = true;
        btnPlus.Tag = position;
        btnPlus.Click += (s, e) => 
        {
            int pos = (int)((Button)s).Tag;
            foreach (var itm in _orderItemLoader.OrderItems)
            {
                if (item.ItemId == GetItemId(position))
                {
                    item.Count += 1;

                    tvDisplayCount.Text = item.Count.ToString();
                    break;
                }
            }
        };
        var btnMinus = itemView.FindViewById<Button>(Resource.Id.buttonMinus);
        btnMinus.Focusable = false;
        btnMinus.FocusableInTouchMode = false;
        btnMinus.Clickable = true;
        btnMinus.Tag = position;
        btnMinus.Click += (s, e) =>
        {
            int pos = (int)((Button)s).Tag;
            var tmpItem = GetItem(pos);
            if (tmpItem != null)
            {
                if (tmpItem.Count > 1)
                    tmpItem.Count -= 1;
                else
                    tmpItem.Count = 0;

                tvDisplayCount.Text = tmpItem.Count.ToString();
            }                
        };
        var imgThumbail = itemView.FindViewById<ImageView>(Resource.Id.imgThumbnail);

        imgThumbail.SetScaleType(ImageView.ScaleType.CenterCrop);
        imgThumbail.SetPadding(8, 8, 8, 8);

        tvDisplayName.Text = item.ItemName;
        tvDisplayName.SetTextColor(Android.Graphics.Color.Black);

        tvDisplayAmount.Text = ServiceManager.GetCurrency(item.Amount);
        tvDisplayAmount.SetTextColor(Android.Graphics.Color.Black);

        tvDisplayCount.SetTextColor(Android.Graphics.Color.Black);

        new SetImageBitmapTask(item, imgThumbail, _activity).Execute("");

        return itemView;
    }

1 个答案:

答案 0 :(得分:0)

我使用ViewHolder改进了列表的性能,我将向您展示一个示例,以便您可以实现

ViewHolder:

public override View GetView(int position, View convertView, ViewGroup parent)
    {
        StoreRecomendadaViewHolder holder = null;
        var view = convertView;
        if (view != null)
            holder = view.Tag as StoreRecomendadaViewHolder;

        var item = items[position];

        if (holder == null)
        {
            holder = new StoreRecomendadaViewHolder();

            if (view == null)
            {
                Activity _activity = context;
                view = _activity.LayoutInflater.Inflate(Resource.Layout.ItemStoreRecomendada, null);
            }

            holder.imgView = view.FindViewById<ImageView>(Resource.Id.imgView);
            holder.txtView = view.FindViewById<TextView>(Resource.Id.txtView);

            view.Tag = holder;
        }
        holder.txtView.SetText(item.NombreComercial, TextView.BufferType.Normal);
        return view;
    }

GetView:

AxisX.Maximum