【问题标题】:Xamarin Android spinner adapter strange bugXamarin Android微调器适配器奇怪的错误
【发布时间】:2017-11-15 19:51:57
【问题描述】:

我在创建自定义微调器适配器时遇到问题。我正在尝试使用列表中每个国家/地区项目左侧的国家/地区图像制作国家/地区选择器。它运行没有错误,但是有一个奇怪的错误,在选择项目并重新打开旋转器时,列表项目会更改位置。

这是我的适配器类:

class SpinnerAdapter : BaseAdapter, ISpinnerAdapter
{
    private string[] data;
    private string[] dataValues;
    private Context context;
    private LayoutInflater inflater;
    private bool drawableFlags;

    public SpinnerAdapter(Context _context, string[] _data, string[] _dataValues, bool _drawableFlags)
        :base()
    {
        context = _context;
        data = _data;
        dataValues = _dataValues;
        drawableFlags = _drawableFlags;

    }
    public override int Count => data.Length;

    public override Java.Lang.Object GetItem(int position)
    {
        return data[position];
    }

    public override long GetItemId(int position)
    {
        //throw new NotImplementedException();
        return (long)position;
    }

    public string GetItemValue(int position)
    {
        return data[position];
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        //throw new NotImplementedException();
        View view = convertView;
        SpinnerAdapterViewHolder holder = null;
        if (view != null)
        {
            holder = view.Tag as SpinnerAdapterViewHolder;
            string name = holder.CountryName.Text;


        }

        if(holder == null)
        {
            holder = new SpinnerAdapterViewHolder();
            var inflater = context.GetSystemService(Context.LayoutInflaterService).JavaCast<LayoutInflater>();
            //replace with your item and your holder items
            //comment back in
            view = inflater.Inflate(Resource.Layout.Spinner_Item_Layout, parent, false);
            holder.CountryName = view.FindViewById<TextView>(Resource.Id.SpinnerItem);
            holder.CountryName.Text = data[position];
            //holder.CountryValue = dataValues[position];
            if (drawableFlags)
            {
                Drawable flag = AppCommon.GetDrawableResourceByName("flag_" + dataValues[position], context);
                flag.SetBounds(10, 10, 10, 10);
                holder.CountryName.SetCompoundDrawablesWithIntrinsicBounds(flag, null, null, null);
            }
            view.Tag = holder;
        }
        return view;
    } 


}

class SpinnerAdapterViewHolder : Java.Lang.Object
{
    public TextView CountryName { get; set; }
    public string CountryValue { get; set; }
}

【问题讨论】:

    标签: android xamarin spinner adapter


    【解决方案1】:

    原来我不需要任何 ViewHolder 的东西。我只是用这个替换了整个 GetView 函数:

    public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var inflater = context.GetSystemService(Context.LayoutInflaterService).JavaCast<LayoutInflater>();
    
            convertView = inflater.Inflate(Resource.Layout.Spinner_Item_Layout, parent, false);
            TextView textview = convertView.FindViewById<TextView>(Resource.Id.SpinnerItem);
            textview.Text = data[position];
            if (drawableFlags)
                {
                    Drawable flag = AppCommon.GetDrawableResourceByName("flag_" + dataValues[position], context);
                    flag.SetBounds(10, 10, 10, 10);
                    textview.SetCompoundDrawablesWithIntrinsicBounds(flag, null, null, null);
                }
    
            return convertView;
        }
    

    问题解决了:D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多