Created
December 29, 2014 19:59
-
-
Save medhdj/4f25d3a7f49ad4ab3499 to your computer and use it in GitHub Desktop.
A custom Spinner with opened and closed event, you can use it to perform some UI changes on the Spinner for example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.Spinner; | |
public class CustomSpinner extends Spinner { | |
// private static final String TAG = "CustomSpinner"; | |
private OnSpinnerEventsListener mListener; | |
private boolean mOpenInitiated = false; | |
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode) { | |
super(context, attrs, defStyleAttr, mode); | |
} | |
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
public CustomSpinner(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public CustomSpinner(Context context, int mode) { | |
super(context, mode); | |
} | |
public CustomSpinner(Context context) { | |
super(context); | |
} | |
public interface OnSpinnerEventsListener { | |
void onSpinnerOpened(Spinner spin); | |
void onSpinnerClosed(Spinner spin); | |
} | |
@Override | |
public boolean performClick() { | |
// register that the Spinner was opened so we have a status | |
// indicator for the activity(which may lose focus for some other | |
// reasons) | |
mOpenInitiated = true; | |
if (mListener != null) { | |
mListener.onSpinnerOpened(this); | |
} | |
return super.performClick(); | |
} | |
public void setSpinnerEventsListener(OnSpinnerEventsListener onSpinnerEventsListener) { | |
mListener = onSpinnerEventsListener; | |
} | |
/** | |
* Propagate the closed Spinner event to the listener from outside. | |
*/ | |
public void performClosedEvent() { | |
mOpenInitiated = false; | |
if (mListener != null) { | |
mListener.onSpinnerClosed(this); | |
} | |
} | |
/** | |
* A boolean flag indicating that the Spinner triggered an open event. | |
* | |
* @return true for opened Spinner | |
*/ | |
public boolean hasBeenOpened() { | |
return mOpenInitiated; | |
} | |
@Override | |
public void onWindowFocusChanged(boolean hasWindowFocus) { | |
super.onWindowFocusChanged(hasWindowFocus); | |
if (hasBeenOpened() && hasWindowFocus) { | |
performClosedEvent(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! This is useful.