Spinner : onItemSelected not called when selected item remains the same

后端 未结 10 654
不思量自难忘°
不思量自难忘° 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 07:48

    I had same problem, I solved it by setting onItemSelectedListener every time adapter changes items.

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

    To make your spinner change despite the value of the last index selected just use a:

    spinner.setSelection(0); 
    

    before your other selection is called

    spinner.setSelection(number); 
    

    this way, the spinner will trigger two times the OnItemSelected event. Just make sure the second time it does whatever you need.

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

    i found a simple solution

    just call setAdapter again in place of notifyDataSetChanged for the second spinner

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

    Rewrote the common solution but with:

    1. androidx in mind
    2. extended from AppCompatSpinner
    3. use built-in OnItemSelectedListener listener instead of creating own one
    4. added initial listener call hack

    Here:

    import android.content.Context;
    
    import androidx.annotation.Nullable;
    import androidx.appcompat.widget.AppCompatSpinner;
    
    
    public class FixedSpinner extends AppCompatSpinner {
        // add other constructors that you need
        public FixedSpinner(Context context, int mode) {
            super(context, mode);
        }
    
        private void processSelection(int position) {
            boolean sameSelected = position == getSelectedItemPosition();
            final OnItemSelectedListener listener = getOnItemSelectedListener();
            if (sameSelected && listener != null) {
                // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
                listener.onItemSelected(this, getSelectedView(), position, getSelectedItemId());
            }
        }
    
        @Override
        public void setSelection(int position) {
            processSelection(position);
            super.setSelection(position);
        }
    
        @Override
        public void setSelection(int position, boolean animate) {
            processSelection(position);
            super.setSelection(position, animate);
        }
    
        @Override
        public void setOnItemSelectedListener(@Nullable OnItemSelectedListener listener) {
            // This hack fixes bug, when immediately after initialization, listener is called
            // To make this fix work, first add data, only then set listener
            // Having done this, you may refresh adapter data as many times as you want
            setSelection(0, false);
            super.setOnItemSelectedListener(listener);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 07:51

    Here a little better implementation:

    public class SpinnerPlus extends Spinner {
        AdapterView.OnItemSelectedListener listener;
    
        public SpinnerPlus(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public void setSelection(int position) {
            super.setSelection(position);
            if (listener != null)
                listener.onItemSelected(this, getSelectedView(), position, 0);
        }
    
        public void setOnItemSelectedEvenIfUnchangedListener(
                AdapterView.OnItemSelectedListener listener) {
            this.listener = listener;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 07:56

    Ok, I finally found a solution, by creating my own class extending Spinner :

    public class MySpinner extends Spinner {
    OnItemSelectedListener listener;
    
    public MySpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    @Override
    public void setSelection(int position) {
        super.setSelection(position);
        if (listener != null)
            listener.onItemSelected(null, null, position, 0);
    }
    
    public void setOnItemSelectedEvenIfUnchangedListener(
            OnItemSelectedListener listener) {
        this.listener = listener;
    }
    }
    
    0 讨论(0)
提交回复
热议问题