Android Spinner OnItemSelected 未使用同一项目调用

标签 android spinner android-spinner

首先,我知道有人多次询问过这个问题,但在较新的 Android 版本上,建议的解决方案似乎不起作用。 我需要我的微调器调用 OnItemSelected,即使用户两次选择相同的项目也是如此。 我发现这个类应该可以解决问题:

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

这实际上是有效的,但它有一个错误:它第一次调用了两次 item :( 谁能告诉我如何解决这个问题?

谢谢伙伴们。

最佳答案

我已经用这个类解决了:

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

还是谢谢你:)

关于Android Spinner OnItemSelected 未使用同一项目调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19734099/

相关文章:

android - 为什么 doctest 在 qpython3 中不起作用

java - Android - 使用 'instanceof' 是确定从 "onItemSelected"中的 Spinner 返回的项目类型的唯一方法吗?

java - 无法在微调器 Android 中检索所选项目

android - 如何像下拉列表一样使用 Android Spinner

android - 如何在 Android 中更改微调器的列表背景颜色

java - 在 android 中记录到磁盘,如循环或环形缓冲区

c# - Xamarin 混淆

android - 多个dex文件定义lorg/apache/commons/io/IOUtils;

java - 计算长度、面积和温度的两个spinner的乘法问题

android - 如何在 Spinner 中保持沉浸式模式?