Android Spinner OnItemSelected not called with the same item

后端 未结 2 2070
花落未央
花落未央 2020-12-10 04:17

First of all, I know that this was asked several time, but on newer android versions it looks like that the suggested solutions doesn\'t work. I need that my spinner call On

相关标签:
2条回答
  • 2020-12-10 04:36

    For me, I extended AppCompatSpinner.

    Also if your Spinneris in XML for layout, remember to change your

    <Spinner...
    

    to

    <com.example.util.NDSpinner...
    
    0 讨论(0)
  • 2020-12-10 04:55

    I've solved using this class:

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

    Thanks anyway :)

    0 讨论(0)
提交回复
热议问题