Android微调OnItemSelected没有调用相同的项目

首先,我知道这是问了好几次,但在较新的android platfom看起来像build议的解决scheme不工作(其他人说的一样)。 我需要我的微调仍然调用OnItemSelected即使当用户select相同的项目两次。 我设法find这个类应该做的伎俩:

public class NDSpinner extends Spinner { private int lastSelected = 0; private static Method s_pSelectionChangedMethod = null; static { try { Class noparams[] = {}; Class targetClass = AdapterView.class; s_pSelectionChangedMethod = targetClass.getDeclaredMethod("selectionChanged", noparams); if (s_pSelectionChangedMethod != null) { s_pSelectionChangedMethod.setAccessible(true); } } catch( Exception e ) { Log.e("Custom spinner, reflection bug:", e.getMessage()); throw new RuntimeException(e); } } 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 protected void onLayout(boolean changed, int l, int t, int r, int b) { if(this.lastSelected == this.getSelectedItemPosition()) testReflectionForSelectionChanged(); if(!changed) lastSelected = this.getSelectedItemPosition(); super.onLayout(changed, l, t, r, b); } public void testReflectionForSelectionChanged() { try { Class noparams[] = {}; s_pSelectionChangedMethod.invoke(this, noparams); } catch (Exception e) { Log.e("Custom spinner, reflection bug: ", e.getMessage()); e.printStackTrace(); } } @Override public void onClick(DialogInterface dialog, int which) { super.onClick(dialog, which); } } 

这实际上工作,但它有一个错误:它第一次调用两次的项目:(任何人都可以知道我怎么能解决这个问题?

感谢队友。

  • 在没有自定义样式的Android Spinner中的项目之间添加更多空间?
  • Android - 删除Spinner下拉箭头
  • 更改工具栏中的微调器样式
  • 更改微调框的背景颜色,但保持箭头
  • 在Android中可以selectHIGHLY FLAWED SPINNER CLASS
  • 自定义Spinners /下拉菜单
  • 不受欢迎的onItemSelected调用
  • 自定义旋转器的间距
  • 我已经解决了使用这个类:

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

    不pipe怎么说,还是要谢谢你 :)

    对我来说,我扩展了AppCompatSpinner。

    另外如果你的Spinneris在用于布局的XML,记得要改变你的

     <Spinner... 

     <com.example.util.NDSpinner...