2017-06-03 119 views
0
  1. 我想修改MvxSpinner設置默認值說 「選擇一個值」。
  2. 我提到this solution覆蓋的方法,但我 無法重寫的方法。

如何在一個優雅的方式如何設置一個默認值MvxSpinner


XML

<Mvx.MvxSpinner 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="30dp" 
     android:background="@drawable/dropdown" 
     local:MvxBind="ItemsSource Cruise; SelectedItem CruiseLineSelected; Visible CruiseAndShipSelectionVisibility" 
     local:MvxItemTemplate="@layout/spinner_cruise_list" 
     local:MvxDropDownItemTemplate="@layout/spinner_cruise_list" 
     android:text="Select a cruise" 
     android:layout_marginBottom="20dp" 
     android:id="@+id/mvxSpinnerCruise" /> 

回答

0

我找到一個簡單的解決方案設置默認文本Spinner,它實現這一目標工作正常,我認爲你可以使用Mvvm通過自己實現這個功能,這裏是 代碼:

public class MySpinnerAdapter<T> : ArrayAdapter<T> 
{ 
    private Context mContext; 
    private int mTextResourceId; 

    public MySpinnerAdapter(Context context, int textViewResourceId):base(context,textViewResourceId) 
    { 
     mContext = Context; 
     mTextResourceId = textViewResourceId; 
    } 

    public override View GetView(int position, View convertView, ViewGroup parent) 
    { 
     View view = base.GetView(position, convertView, parent); 
     if (position == Count) 
     { 
      TextView a = (view.FindViewById<TextView>(Android.Resource.Id.Text1)); 
      a.Text = ""; 

      TextView b = (view.FindViewById<TextView>(Android.Resource.Id.Text1)); 
      b.Hint = GetItem(Count).ToString(); //"Hint to be displayed" 
     } 
     return view; 
    } 

    public override int Count => (base.Count - 1); 
} 

當使用它:

spinner = FindViewById<Spinner>(Resource.Id.spinner); 
var adapter = new MySpinnerAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem); 

adapter.Add("php"); 
adapter.Add("java"); 
adapter.Add("C++"); 
adapter.Add("C"); 
adapter.Add("Select a cruise"); 

spinner.Adapter = adapter; 
spinner.SetSelection(adapter.Count); //display hint 

HereSpinner