I could not find a way to get this behaviour with the spinner so the only thing that worked for me was to use the spinner (custom) adapter instead:
public interface SpinnerListener {
void onSpinnerExpanded();
void onSpinnerCollapsed();
}
Then a custom adapter can be written that just grabs the “spinner expanded” view and adds a listener to it to listen for “expand” and “collapse” events. The custom adapter I used is:
public class ListeningArrayAdapter<T> extends ArrayAdapter<T> {
private ViewGroup itemParent;
private final Collection<SpinnerListener> spinnerListeners = new ArrayList<SpinnerListener>();
public ListeningArrayAdapter(Context context, int resource, T[] objects) {
super(context, resource, objects);
}
// Add the rest of the constructors here ...
// Just grab the spinner view (parent of the spinner item view) and add a listener to it.
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (isParentTheListView(parent)) {
itemParent = parent;
addFocusListenerAsExpansionListener();
}
return super.getDropDownView(position, convertView, parent);
}
// Assumes the item view parent is a ListView (which it is when a Spinner class is used)
private boolean isParentTheListView(ViewGroup parent) {
return (parent != itemParent && parent != null && ListView.class.isAssignableFrom(parent.getClass()));
}
// Add a focus listener to listen to spinner expansion and collapse events.
private void addFocusListenerAsExpansionListener() {
final View.OnFocusChangeListener listenerWrapper = new OnFocusChangeListenerWrapper(itemParent.getOnFocusChangeListener(), spinnerListeners);
itemParent.setOnFocusChangeListener(listenerWrapper);
}
// Utility method.
public boolean isExpanded() {
return (itemParent != null && itemParent.hasFocus());
}
public void addSpinnerListener(SpinnerListener spinnerListener) {
spinnerListeners.add(spinnerListener);
}
public boolean removeSpinnerListener(SpinnerListener spinnerListener) {
return spinnerListeners.remove(spinnerListener);
}
// Listener that listens for 'expand' and 'collapse' events.
private static class OnFocusChangeListenerWrapper implements View.OnFocusChangeListener {
private final Collection<SpinnerListener> spinnerListeners;
private final View.OnFocusChangeListener originalFocusListener;
private OnFocusChangeListenerWrapper(View.OnFocusChangeListener originalFocusListener, Collection<SpinnerListener> spinnerListeners) {
this.spinnerListeners = spinnerListeners;
this.originalFocusListener = originalFocusListener;
}
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (originalFocusListener != null) {
originalFocusListener.onFocusChange(view, hasFocus); // Preserve the pre-existing focus listener (if any).
}
callSpinnerListeners(hasFocus);
}
private void callSpinnerListeners(boolean hasFocus) {
for (SpinnerListener spinnerListener : spinnerListeners) {
if (spinnerListener != null) {
callSpinnerListener(hasFocus, spinnerListener);
}
}
}
private void callSpinnerListener(boolean hasFocus, SpinnerListener spinnerListener) {
if (hasFocus) {
spinnerListener.onSpinnerExpanded();
}
else {
spinnerListener.onSpinnerCollapsed();
}
}
}
}
Then when I use a spinner in my activity or fragment all I had to do was to set the spinner adapter to the above custom adapter:
private ListeningArrayAdapter<CharSequence> adapter;
private Spinner buildSpinner() {
final CharSequence[] items = {"One", "Two", "Three"};
final Spinner spinner = (Spinner)getActivity().getLayoutInflater().inflate(R.layout.item_spinner, null);
adapter = new ListeningArrayAdapter<CharSequence>(getActivity(), R.layout.item_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.addSpinnerListener(new TestSpinnerListener(getActivity())); // Add your own spinner listener implementation here.
spinner.setAdapter(adapter);
return spinner;
}
I know that this is a bit of a hack and a a bit brittle but it worked for me. It would be much better if the Spinner class had all this functionality build in and allowed you to set an expand-collapse listener. For the time being I will have to do with this hack.
onClickListner
for spinner. Since spinner open when we click(tap) on it. And to chcek where the spinner is open or not we can useonItemSelectedListner
callbacks. Tell me if you want further code example. – FlagstadonNothingSelected
withonItemSelectedListner
. If you will ask further help i will write complete logic for you. – FlagstadSpinner
is opened or closed? – Underpay