android - Spinner: get state or get notified when opens -
is possible know whether spinner
open or closed? better if there sort of onopenlistener spinners.
i've tried using onitemselectedlistener this:
spinnertype.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { executesomething(); } @override public void onnothingselected(adapterview<?> parent) { log.d("nothing" , "selected"); } });
i can know window close if selected (in executesomething()). don't notified if click outside of dialog, closes spinner
another option watch events extend spinner
class , use 1 of methods(performclick()
trigger dialog/popup) followed monitoring focus of window holding custom spinner
. should provide wanted closed event possible finishing possibilities(for either dialog or dropdown mode).
the custom spinner
class:
public class customspinner extends spinner { /** * interface client of spinner use receive * open/closed events spinner. */ public interface onspinnereventslistener { /** * callback triggered when spinner opened. */ void onspinneropened(spinner spinner); /** * callback triggered when spinner closed. */ void onspinnerclosed(spinner spinner); } private onspinnereventslistener mlistener; private boolean mopeninitiated = false; // implement spinner constructors need @override public boolean performclick() { // register spinner opened have status // indicator when container holding spinner may lose focus mopeninitiated = true; if (mlistener != null) { mlistener.onspinneropened(this); } return super.performclick(); } /** * register listener listen events. */ public void setspinnereventslistener( onspinnereventslistener onspinnereventslistener) { mlistener = onspinnereventslistener; } /** * propagate closed spinner event listener outside if needed. */ public void performclosedevent() { mopeninitiated = false; if (mlistener != null) { mlistener.onspinnerclosed(this); } } /** * boolean flag indicating spinner triggered open event. * * @return true opened spinner */ public boolean hasbeenopened() { return mopeninitiated; } public void onwindowfocuschanged (boolean hasfocus) { if (hasbeenopened() && hasfocus) { performclosedevent(); } } }
Comments
Post a Comment