Spinner : onItemSelected not called when selected item remains the same

后端 未结 10 646
不思量自难忘°
不思量自难忘° 2020-11-28 07:12

I have a OnItemSelectedListener for my Spinner, but it is not called when the selected item is the same as the previous one. Apparently the O

相关标签:
10条回答
  • 2020-11-28 08:04

    I found out this work instead of the one provided

    /** Spinner extension that calls onItemSelected even when the selection is the same as its previous value */
    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());
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-28 08:05

    If it's still actual, correct call of the callback should be

    @Override
    public void setSelection(int position) {
        super.setSelection(position);
        if(listener != null)
            listener.onItemSelected(this, getChildAt(position), position, 0);
    }
    

    Martin

    0 讨论(0)
  • 2020-11-28 08:07

    Simplest solution :

    spinner.performItemClick(view,position,id)

    0 讨论(0)
  • 2020-11-28 08:08

    Here is a better implementation -

    Custom Spinner Class -

    import android.content.Context;
    import android.util.AttributeSet;
    import androidx.appcompat.widget.AppCompatSpinner;
    
    public class CSpinner extends AppCompatSpinner {
    
        private int lastPosition = 0;
    
        public CSpinner(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public void setSelection(int position) {
            super.setSelection(position);
            boolean sameSelected = lastPosition == getSelectedItemPosition();
            OnItemSelectedListener onItemSelectedListener = getOnItemSelectedListener();
            if (sameSelected && onItemSelectedListener != null) {
                onItemSelectedListener.onItemSelected(this, getSelectedView(), position, getSelectedItemId());
            }
            lastPosition = position;
        }
    }
    

    Setting Listener -

    spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.d("onItemSelected", String.valueOf(position));
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                
            }
        });
    
    0 讨论(0)
提交回复
热议问题