2017-11-15 5 views
0

カスタムスピナーアダプターの作成中に問題が発生しました。私はリストの各国の項目の左にある国のイメージを持つ国の選択をしようとしています。それはエラーなしで実行されますが、項目を選択してスピンナーを再び開くときにリスト項目が位置を変える不思議なバグがあります。Xamarin Androidスピナーアダプターの不具合のバグ

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

答えて

0

私はViewHolderのもののいずれかを必要としないが判明:

は、ここに私のアダプタクラスです。

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

私はこれでgetViewメソッド関数の全体を置き換えます