我的微调器中有一个过滤器复选框,用于过滤任务是否完成。如果我选中一个项目并选中过滤器复选框,即使列表已经是新的,微调器中的选定值(选中的单选按钮(仍然会被选中。
代码段如果切换了过滤器开关,如何取消选中微调器中的复选项?
public class CustomSpinnerAdapter : ArrayAdapter<String> {
String type = String.Empty;
List<string> orderList = new List<string>();
/// <summary>
/// CUstom Spinner Array Adapter constructor
/// </summary>
/// <returns></returns>
public CustomSpinnerAdapter(Context context, int textViewResourceId, List<String> objects, String type)
: base(context, textViewResourceId, objects) {
this.type = type;
this.orderList = objects;
}
/// <summary>
/// returns the total array count minus one
/// </summary>
/// <returns>int</returns>
public override int Count
{
get
{
return base.Count;
}
}
public override View GetDropDownView(int position, View convertView, ViewGroup parent)
{
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
View view = null;
if (position == 0)
{
if(Constants.ORDER_LIST == type) {
view = inflater.Inflate(Resource.Layout.CustomSpinnerView, null, true);
Switch filterOrder = (Switch) view.FindViewById(Resource.Id.switch_filterOrder);
filterOrder.Checked = Constants.ORDER_SWITCH_CLOSED == Constants.ORDER_SWITCH;
filterOrder.CheckedChange += delegate (object sender, CompoundButton.CheckedChangeEventArgs e)
{
saveOrderChecked(e.IsChecked);
PageTaskSearchView pageTaskSearchView = new PageTaskSearchView();
orderList = pageTaskSearchView.retrieveOrderlist(Constants.ORDER_SWITCH);
base.Clear();
base.AddAll(orderList);
base.NotifyDataSetChanged();
};
}
else
{
LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(0, 0);
view = base.GetDropDownView(position, convertView, parent);
TextView tv = (TextView)view;
tv.SetHeight(0);
tv.LayoutParameters = layoutparams;
tv.Visibility = ViewStates.Gone;
view = tv;
}
}
else
{
view = base.GetDropDownView(position, null, parent);
}
parent.VerticalScrollBarEnabled = false;
return view;
}
public static void saveOrderChecked(bool saveChecked)
{
Constants.ORDER_SWITCH = saveChecked ? Constants.ORDER_SWITCH_CLOSED : 0;
Constants.ORDER_SWITCH_TRIGGER = true;
}
}
}
orderSpinner = (Spinner)view.FindViewById(Resource.Id.spinner_workOrder);
orderListResult = new List<string>();
clearOrderIDListSelection();
orderListResult.Clear();
orderListResult.Add(Constants.EMPTY_STRING);
foreach (OrderModel orderData in orderIds)
{
orderListResult.Add(orderData.IDNo);
}
OrderIdAdapter = new CustomSpinnerAdapter(context, Resource.Layout.ListItem, orderListResult, Constants.ORDER_LIST);
OrderIdAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
CreateCustomSpinner(orderSpinner, orderListResult, OrderIdAdapter);
用于清除微调器选择的方法
public void clearOrderIDListSelection()
{
RemoveItemSeclectedEvent(this.orderSpinner);
CreateCustomSpinner(orderSpinner, orderListResult, OrderIdAdapter);
orderSpinner.SetSelection(-1);
}
正如我所发现的,spinner被设计为选中它的一个项,没有用于取消选择的可选api,doc说SetSelection方法接受了一个从0开始的索引,所以-1不起作用,你需要自己实现取消选择的微调器/适配器,但通常的方法是在列表顶部添加一个空白项目或写着"无"/"选择一个"的项目。
在您的代码中,您可以添加一个空字符串作为orderList的第一个元素,当切换时,只需设置:
spinner.SetSelection(0)
此外,这里有一些人的工作围绕着将spinner设置为none。