已选择的Android微调器未使用同一项调用

已选择的Android微调器未使用同一项调用,android,spinner,android-spinner,Android,Spinner,Android Spinner,首先,我知道有人多次问过这个问题,但在较新的android版本上,建议的解决方案似乎不起作用。 我需要选择微调器调用,即使用户选择同一项两次。 我发现这个类应该可以做到这一点: public class NDSpinner extends Spinner { private int lastSelected = 0; private static Method s_pSelectionChangedMethod = null; static {

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

    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);
    }
}
这个事实是可行的,但它有一个bug:它第一次调用两次该项:( 有人能告诉我怎么解决这个问题吗


谢谢各位同学。

我已经通过这门课解决了以下问题:

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

无论如何谢谢:)

对于我,我扩展了AppCompatSpinner

另外,如果您的喷丝头是XML格式的布局,请记住更改您的

<Spinner...

经过几个小时的研究终于找到了答案。你帮我节省了很多时间。谢谢,伙计。我怎么能在我的活动中使用它呢?@HammadNasir它只是一个定制的微调器。只需使用NDSpinner对象而不是经典的Spinner.boolean sameSelected=position==getSelectedItemPosition();getSelectedItemPosition()方法返回与position相同的值,请检查我的post-better实现-
<com.example.util.NDSpinner...