【问题标题】:Android Spinner OnItemSelected not called with the same itemAndroid Spinner OnItemSelected 未使用相同的项目调用
【发布时间】:2013-11-13 02:05:13
【问题描述】:

首先,我知道这被问了好几次,但在较新的 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);
    }
}

这个事实有效,但它有一个错误:它第一次调用了两次该项目:( 谁能告诉我如何解决这个问题?

谢谢小伙伴们。

【问题讨论】:

标签: android spinner android-spinner


【解决方案1】:

我已经用这个类解决了:

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

还是谢谢:)

【讨论】:

  • 经过几个小时的研究终于找到了你的答案。你节省了我很多时间。谢谢大佬。
  • 如何在我的活动中使用它?
  • @HammadNasir 它只是一个自定义 Spinner。只需使用 NDSpinner 对象而不是经典的 Spinner。
  • 布尔sameSelected = position == getSelectedItemPosition();始终为 TRUE,getSelectedItemPosition() 方法返回与位置相同的值,查看我的帖子 - stackoverflow.com/a/62894086/6244429
【解决方案2】:

对我来说,我扩展了 AppCompatSpinner。

另外,如果您的 Spinneris 在 XML 中进行布局,请记住更改您的

<Spinner...

<com.example.util.NDSpinner...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多