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
Rewrote the common solution but with:
AppCompatSpinnerOnItemSelectedListener listener instead of creating own oneHere:
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);
}
}