微调:当select的项目保持不变时,onItemSelected不会被调用

我有一个OnItemSelectedListener为我的Spinner ,但它不会被调用,当选定的项目是一样的前一个。 显然, OnClickListener不是Spinner的选项。 每当用户点击一个项目时,我都需要抓住它。 任何想法?

也许这个SpinnerActionBar内部的事实干扰了正常的行为?

 @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.tracklist_menu, menu); Spinner spinner = (Spinner) menu.findItem(R.id.option_ordering_spinner) .getActionView(); spinner.setAdapter(mSpinnerAdapter); spinner.setSelection(PrefsHelper.getOrderingSpinnerPos(prefs)); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String str = "selected"; System.out.println(str); if (optionMenuInitialized) { switch (position) { case 0: // rdm getActivity() .sendBroadcast( new Intent( MyIntentAction.DO_RESHUFFLE_PLAYLIST)); smp.setCurrentTracklistCursorPos(-1); trackAdapter.notifyDataSetChanged(); break; case 1: // artist getActivity() .sendBroadcast( new Intent( MyIntentAction.DO_ORDER_PLAYLIST_BY_ARTIST)); smp.setCurrentTracklistCursorPos(-1); trackAdapter.notifyDataSetChanged(); break; case 2: // folder getActivity() .sendBroadcast( new Intent( MyIntentAction.DO_ORDER_PLAYLIST_BY_FOLDER)); smp.setCurrentTracklistCursorPos(-1); trackAdapter.notifyDataSetChanged(); break; } PrefsHelper.setOrderingSpinnerPos(prefEditor, position); prefEditor.commit(); } optionMenuInitialized = true; } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } 

好吧,我终于find了一个解决scheme,通过创build我自己的类扩展微调:

 public class MySpinner extends Spinner { OnItemSelectedListener listener; public MySpinner(Context context, AttributeSet attrs) { super(context, attrs); } @Override public void setSelection(int position) { super.setSelection(position); if (listener != null) listener.onItemSelected(null, null, position, 0); } public void setOnItemSelectedEvenIfUnchangedListener( OnItemSelectedListener listener) { this.listener = listener; } } 

我find了这个工作,而不是提供的

 /** Spinner extension that calls onItemSelected even when the selection is the same as its previous value */ public class NDSpinner extends Spinner { public NDSpinner(Context context) { super(context); } public NDSpinner(Context context, AttributeSet attrs) { super(context, attrs); } public NDSpinner(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void setSelection(int position, boolean animate) { boolean sameSelected = position == getSelectedItemPosition(); super.setSelection(position, animate); if (sameSelected) { // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId()); } } @Override public void setSelection(int position) { boolean sameSelected = position == getSelectedItemPosition(); super.setSelection(position); if (sameSelected) { // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId()); } } } 

这里稍微好一点的实现:

 public class SpinnerPlus extends Spinner { AdapterView.OnItemSelectedListener listener; public SpinnerPlus(Context context, AttributeSet attrs) { super(context, attrs); } @Override public void setSelection(int position) { super.setSelection(position); if (listener != null) listener.onItemSelected(this, getSelectedView(), position, 0); } public void setOnItemSelectedEvenIfUnchangedListener( AdapterView.OnItemSelectedListener listener) { this.listener = listener; } } 

如果它仍然是实际的,那么callback应该是正确的

 @Override public void setSelection(int position) { super.setSelection(position); if(listener != null) listener.onItemSelected(this, getChildAt(position), position, 0); } 

马丁

为了让你的微调改变,尽pipe最后select的索引值只使用一个:

 spinner.setSelection(0); 

在你的其他select被调用之前

 spinner.setSelection(number); 

这样,微调器将触发两次OnItemSelected事件。 只要确保第二次它做任何你需要的。

最简单的解决scheme

spinner.performItemClick(视图,位置,ID)